Support

Account

Home Forums ACF PRO Archive with ajax custom field filter : Error 500

Solving

Archive with ajax custom field filter : Error 500

  • Hi,

    I try to create CPT archive with ACF ajax filter with this link : https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/

    When I select a filter, a page opens with this message “array(2) { [0]=> string(10) “alcool1” [1]=> string(7) “alcool2″ }”
    And this in my console :

    (index):226 GET https://mywebsite.fr/cocktails?alcool=alcool1,alcool2&caracteristiques=caracteristiques 500
    (anonymous) @ (index):226
    Navigated to https://mywebsite.fr/cocktails?alcool=alcool1,alcool2&caracteristiques=caracteristiques

    My code :

    functions.php

    
    // array of filters (field key => field name)
    $GLOBALS['my_query_filters'] = array(
    	//'field_1'	=> 'city',
    	//'field_2'	=> 'bedrooms'
    
    	'alcool'	=> 'alcool',
    	'caracteristiques'	=> 'caracteristiques'
    );
    
    // 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 ]);
    
    		var_dump($value);
    
    		// append meta query
    		$meta_query[] = array(
    			'key'		=> $name,
    			'value'		=> $value,
    			'compare'	=> 'LIKE',
    		);
    
    	}
    
    	// update meta query
    	$query->set('meta_query', $meta_query);
    
    }
    

    archive-mycpt.php

    <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 type="text/javascript">
    (function($) {
    
    // change
    $('#archive-filters').on('change', 'input[type="checkbox"]', function(){
    
    console.log('ok');
    
    // vars
    var url = '<?php echo home_url('cocktails'); ?>';
    	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>

    Thank you !

  • This could be anything in the PHP code. Turn on error logging in WP and look at the log to see if you can figure out the problem in the php code https://codex.wordpress.org/WP_DEBUG, but I would suspect it has something to do with trying var_dump($value); during an ajax request.

  • Thank you for your response !
    My debug.log file steal empty. I remove the var_dump, now when I select a filter I have an error 500 page.

  • Well, my log now works. I have this error :
    PHP Fatal error: Uncaught Error: [] operator not supported for strings

    I change my code :

    $meta_query[] = array(
        'key'		=> $name,
        'value'		=> $value,
        'compare'	=> 'LIKE',
    );

    to :

    
    $meta_query = [];
    $meta_query[] = array(
        'key'		=> $name,
        'value'		=> $value,
        'compare'	=> 'LIKE',
    );
    

    Now I have this error in my log :

    PHP Warning: trim() expects parameter 1 to be string, array given in /home/vingtquazk/www/24prod/test/wp-includes/class-wp-meta-query.php on line 613

    I know I can use trim only with a string, but I think I have to use an array…

  • Sorry for not getting back to you on this. I’ve looked at it and I can’t tell by looking at the code you’ve supplied why you are getting this error.

  • No problem. I updated my code :

    $GLOBALS['my_query_filters'] = array(
    	'field_5cb6ef1f75209'  => 'alcool',
    );
    
    // 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;
    
    	// 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);
    	return;
    
    }
    

    But it doesn’t work. I don’t have any result when I select a checkbox filter. I think checkbox field is stored as serialized array, so I can not use the IN operator and array (if I select multiple checkbox filters).
    I think I have to create a loop with an OR relation in my meta_query.

  • Yes, you do. You cannot do “IN” with an ACF checkbox field. You need to do “OR” relation with a “LIKE” query for every value that you are looking for.

  • Well, if it can help somebody, this is my working code :

    // array of filters (field key => field name)
    $GLOBALS['my_query_filters'] = array(
    	'field_5cb6ef1f75209'  => 'alcool',
    	'field_5cb74afb68539'  => 'caracteristiques',
    );
    
    // 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
    	$meta_query = array(
    		'relation' => 'AND',
    	);
    	$i = 0;
    	foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
    
    		// continue if not found in url
    		if( empty($_GET[ $name ]) ) {
    			continue;
    		}
    
    		$i++;
    
    		$meta_query[$i] = array(
    			'relation' => 'OR',
    		);
    
    		// get the value for this filter
    		$values = explode( ",", $_GET[ $name ] );
    
    		foreach( $values as $value ) {
    			$meta_query[$i][] = array(
    				'key'      => $name,
    				'value'    => $value,
    				'compare'  => 'LIKE',
    			);
    
    		}
    	}
    	// update meta query
    	$query->set('meta_query', $meta_query);
    	return;
    
    }
    

    Just a question, is there a way to “ajaxify” this ? With no page reload ?

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

The topic ‘Archive with ajax custom field filter : Error 500’ is closed to new replies.