Support

Account

Home Forums General Issues Filtering Archives Using Checkbox Custom Fields Reply To: Filtering Archives Using Checkbox Custom Fields

  • @elliot *Exhale* I feel to cry! After a whole day of staring at my functions.php, I think I may have finally cracked it!

    add_action('pre_get_posts', 'my_pre_get_posts');
     
    function my_pre_get_posts( $query ) {
    	if( is_admin() ) {
    		return;
    	}
     
    	$book_format = $query->get('meta_query');
     
            if( !empty($_GET['type']) ) {
            
    	    	$book_format[] = array(
                    'key'		=> 'type',
                    'value'		=> $_GET['type'],
                    'compare'	=> 'LIKE',
                );
            }
     
    	$query->set('meta_query', $book_format);
     
    	return;
    }

    Now when I append this to my url:

    dev.com/books?=home

    it displays an archive of all my books with the ‘type’ of ‘home’. This is fantastic!

    The only problem I’m having now is, separating these in the url with the ‘|’ character doesn’t seem to yield any results. Any idea why that might be happening, or is that normal?