Support

Account

Home Forums General Issues Creating a WP archive with custom field filter… and Array to string error

Solving

Creating a WP archive with custom field filter… and Array to string error

  • Hey everyone.

    I’m using the code from this tutorial: https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/

    I’ve only changed $GLOBALS array with my fields. The code looks like it’s working but it’s showing me a notice

    Notice: Array to string conversion in /home/xxx/domains/xxx/public_html/wp-content/plugins/advanced-custom-fields/includes/fields/class-acf-field-radio.php on line 74

    74 line is the last one:

    	function render_field( $field ) {
    
    		// vars
    		$i = 0;
    		$e = '';
    		$ul = array( 
    			'class'				=> 'acf-radio-list',
    			'data-allow_null'	=> $field['allow_null'],
    			'data-other_choice'	=> $field['other_choice']
    		);
    		
    		
    		// append to class
    		$ul['class'] .= ' ' . ($field['layout'] == 'horizontal' ? 'acf-hl' : 'acf-bl');
    		$ul['class'] .= ' ' . $field['class'];
    		
    		
    		// select value
    		$checked = '';
    		$value = strval($field['value']);
    		

    Functions.php code:

    // array of filters (field key => field name)
    $GLOBALS['my_query_filters'] = array(
    	'field_5d9d111193121'	=> 'czas_przygotowania',
    	'field_5d9d10cf93120'	=> 'poziom_trudnosci'
    );
    
    // action
    add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);
    
    function my_pre_get_posts( $query ) {
    
    	// bail early if is in admin
    	if( is_admin() ) return;
    
    	// bail early if not main query
    	// - allows custom code / plugins to continue working
    	if( !$query->is_main_query() ) return;
    
    	// get meta query
    	$meta_query = $query->get('meta_query');
    
    	// loop over filters
    	foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
    		// continue if not found in url
    		if( empty($_GET[ $name ]) ) {
    
    			continue;
    
    		}
    
    		// get the value for this filter
    		// eg: http://www.website.com/events?city=melbourne,sydney
    		$value = explode(',', $_GET[ $name ]);
    
            // append meta query
    
            $meta_query = array(
                array(
                    'key'     => $name,
                    'value'       => $value,
                    'compare'  => 'IN',
                )
            );
    
    	}
    
    	// update meta query
    	$query->set('meta_query', $meta_query);
    
    }
    

    archive.php code:

    	<div id="archive-filters">
    	<?php foreach( $GLOBALS['my_query_filters'] as $key => $name ):
    
    		// get the field's settings without attempting to load a value
    		$field = get_field_object($key, false, false);
    
    		// set value if available
    		if( isset($_GET[ $name ]) ) {
    
    			$field['value'] = explode(',', $_GET[ $name ]);
    
    		}
    
    		// create filter
    		?>
    		<div class="filter" data-filter="<?php echo $name; ?>">
    			<?php create_field( $field ); ?>
    		</div>
    
    	<?php endforeach; ?>
    	</div>

    Script:

    <script type="text/javascript">
    (function($) {
    
    	// change
    	$('#archive-filters').on('change', 'input[type="radio"]', function(){
    
    		// vars
    		var url = '<?php echo home_url('przepisy'); ?>';
    			args = {};
    
    		// loop over filters
    		$('#archive-filters .filter').each(function(){
    
    			// vars
    			var filter = $(this).data('filter'),
    				vals = [];
    
    			// find checked inputs
    			$(this).find('input:checked').each(function(){
    
    				vals.push( $(this).val() );
    
    			});
    
    			// append to args
    			args[ filter ] = vals.join(',');
    
    		});
    
    		// update url
    		url += '?';
    
    		// loop over args
    		$.each(args, function( name, value ){
    
    			url += name + '=' + value + '&';
    
    		});
    
    		// remove last &
    		url = url.slice(0, -1);
    
    		// reload page
    		window.location.replace( url );
    
    	});
    
    })(jQuery);
    </script>

    What I’m doing wrong? I’ve tried to use input[type=”checkbox”] but I’ve found an answer, that’s bad solution and it’s not working (url: https://wordpress.stackexchange.com/questions/336715/meta-query-in-doesnt-work-with-acf-checkbox-filter).

    I’ve searched many topic before I decided to create a new one. Please help me.

  • I’ve fixed the problem, because I can’t use explode() and radio buttons. Now I’m using checkboxes, nevermind.

    But I have an another problem, because the last checkbox (in the first and second checkboxes group) is always checked and label class is “selected”

    <li>
    <label class="selected">
    <input type="checkbox" id="acf-field_5d9d111193121-ponad_60_minut" name="acf[field_5d9d111193121][]" value="ponad_60_minut" checked="checked"> ponad 1h
    </label>
    </li>

    For example. I have a data-filter: colors, and three checkboxes:
    – BLUE
    – RED
    GREEN

    and green is default checked, so when I check red I have two values checked.

    What I can do to default disable those checkboxes?

    The function which is creating the checkboxes is:

    <?php create_field( $field ); ?>

  • I can’t edit posts…

    It’s displaying the values from first looped article. How to fix it?

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.