Support

Account

Home Forums General Issues WP archive with many custom fields filter

Unread

WP archive with many custom fields filter

  • Hello, i testing now ACF. I creating a WP archive with custom field filter by documentation video. http://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/. And it`s all working.

    But now i have little problem, how to make a second parameter?

    Ex: At documentation we use only one checkbox bedrooms, but i want add livingrooms(or bathrooms).

    Checkbox Filter(archive-houses.php) must be like

    bedrooms 1 | 2 | 3
    bathrooms 1 | 2 | 3

    I added bathroom, and now can fileter by http://www.website/blabla/?badrooms=1,2&bathrooms=1.

    
    add_action('pre_get_posts', 'my_pre_get_posts');
    
    function my_pre_get_posts( $query )
    {
    	// validate
    	if( is_admin() )
    	{
    		return;
    	}
    
    	if( !$query->is_main_query() )
    	{
    		return;
    	}
    
    	// get original meta query
    	$meta_query = $query->get('meta_query');
    
            // allow the url to alter the query
            // eg: http://www.website.com/events?location=melbourne
            // eg: http://www.website.com/events?location=sydney
            if(( !empty($_GET['bedrooms']) ) && ( !empty($_GET['bathrooms']) ))
            {
            	$bedrooms = explode(',', $_GET['bedrooms']);
    			$bathrooms = explode(',', $_GET['bathrooms']);
    
            	//Add our meta query to the original meta queries
    	    	$meta_query[] = array(
                    'key'		=> 'bedrooms',
                    'value'		=> $bedrooms,
                    'compare'	=> 'IN',
                );
    			$meta_query[] = array(
                    'key'		=> 'bathrooms',
                    'value'		=> $bathrooms,
                    'compare'	=> 'IN',
                );
            }
    
    	// update the meta query args
    	$query->set('meta_query', $meta_query);
    
    	// always return
    	return;
    
    }

    Now i found one more problem. If badrooms=1 and bathroom=3, but we have no bathroom=3 at badroom=1. We have error.
    Warning: Invalid argument supplied for foreach() in Z:\home\balbla.com\www\wp-content\themes\twentytwelve\archive-house.php on line 35

Viewing 1 post (of 1 total)

The topic ‘WP archive with many custom fields filter’ is closed to new replies.