Home Forums ACF PRO WP archive with custom field filter : not working


WP archive with custom field filter : not working

    • hedi

    • July 31, 2014 at 7:40 pm

    With acf5, i think there is a bug with creating a WP archive with custom field filter.
    I created exactly the same custom fields (bedrooms), pasted exactly the same code on page and function which are eplained on your documentation :

    It return on frontend :

    Notice: Undefined index: bedrooms
    on my page i have (exactly the same as on the documentation page):
    $field = get_field_object('bedrooms');

    Warning: Invalid argument supplied for foreach()
    the foreach loop is (exactly the same as on the documentation page):
    foreach( $field['choices'] as $choice_value => $choice_label ):

    the entire code :


    	$field = get_field_object('bedrooms');
    	$values = explode(',', $_GET['bedrooms']);
    		<?php foreach( $field['choices'] as $choice_value => $choice_label ): ?>
    				<input type="checkbox" value="<?php echo $choice_value; ?>" <?php if( in_array($choice_value, $values) ): ?>checked="checked"<?php endif; ?> /> <?php echo $choice_label; ?></li>
    		<?php endforeach; ?>


    add_action('pre_get_posts', 'my_pre_get_posts');
    function my_pre_get_posts( $query )
    	// validate
    	if( is_admin() )
    	if( !$query->is_main_query() )
    	// get original meta query
    	$meta_query = $query->get('meta_query');
            // allow the url to alter the query
            // eg:
            // eg:
            if( !empty($_GET['bedrooms']) )
            	$bedrooms = explode(',', $_GET['bedrooms']);
            	//Add our meta query to the original meta queries
    	    	$meta_query[] = array(
                    'key'		=> 'bedrooms',
                    'value'		=> $bedrooms,
                    'compare'	=> 'IN',
    	// update the meta query args
    	$query->set('meta_query', $meta_query);
    	// always return

    Is the documentation page out of date?

    Thank you

    • Elliot

    • August 6, 2014 at 1:12 pm

    Hi @hedi

    The PHP error you have listed states that there is no $_GET['bedrooms'] when loading the page.

    You will need to add some logic to determine if the bedrooms isset, which I have updated the linked documentation to do.


    • hedi

    • August 6, 2014 at 10:39 pm

    thank you

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.