• Hey

    I have a custom post type of “Festival” and i need to be able to search that post type by fields setup with ACF (advanced custom fields).

    When i initially got this search function working i noticed my menu disappeared. I later found that i needed to check if the query->is_main_query to ensure i do not alter the other post type queries.

    That makes sense however if run the below code all i get are the non custom post types (regular blog articles) and the menu works again. Removing the check for is_main_query() gets me the custom post types but no menu.

    Any advice is extremely welcome. Im fairly new to wordpress codex but i am knowledgable in PHP

    function my_pre_get_posts( $query ) {
    	//* if within admin, return
        if ( is_admin() ) {
            return;
    
        }
        //* if not main query, return
        elseif ( ! $query->is_main_query() ) {
            return;
        }
    
    	else {
    		if(	(isset($_GET['fs_date_from']) && $_GET['fs_date_from'] != '') 		||
    			(isset($_GET['fs_date_to']) && $_GET['fs_date_to'] != '') 			||
    			(isset($_GET['fs_music_genre']) && $_GET['fs_music_genre'] != '') 	||
    			(isset($_GET['fs_keyword']) && $_GET['fs_keyword'] != '')			||
    			(isset($_GET['fs_festival_category']) && $_GET['fs_festival_category'] != '')) {
    
    			if(isset($_GET['fs_date_from']) && $_GET['fs_date_from'] != '') {
    				$date_from 		= date('Ymd',strtotime(sanitize_text_field($_GET['fs_date_from'])));
    				$search_array['fs_date_from'] = array(	'key' 		=> 'start_date',
    														'value' 	=> $date_from,
    														'compare' 	=> '>='
    														);
    			}
    			if(isset($_GET['fs_date_to']) && $_GET['fs_date_to'] != '') {
    				$date_to 		= date('Ymd',strtotime(sanitize_text_field($_GET['fs_date_to'])));
    				$search_array['fs_date_to'] = array(	'key' 		=> 'end_date',
    														'value' 	=> $date_to,
    														'compare' 	=> '<='
    														);
    			}
    			$keyword 		= sanitize_text_field($_GET['fs_keyword']);
    			if($keyword != '') {
    				$query->set('s',$keyword);
    			}
    
    			$music_genre	= sanitize_text_field($_GET['fs_music_genre']);
    			if($music_genre != '') {
    				$tax_query['music_genre'] = array(	'taxonomy' 	=> 'music_genre',
    													'field'		=> 'id',
    													'terms' 	=> array($music_genre),
    													'operator'  => 'IN'
    													);
    			}
    			$festival_category	= sanitize_text_field($_GET['fs_festival_category']);
    			if($festival_category != '') {
    				$tax_query['fs_festival_category'] = array(	'taxonomy' 	=> 'festival_category',
    															'field'		=> 'id',
    															'terms' 	=> array($festival_category),
    															'operator'  => 'IN'
    															);
    			}
    			$search_array['relation']	= (($date_from != '' && $date_to != '')? "AND" : "");
    			$query->set('posts_per_page', 10);
    			$query->set('post_type'		, 'festival');
    			$query->set('meta_query'	, $search_array);
    			$query->set('tax_query'		, $tax_query);
    
    		}
    		else {
    			$query->set('posts_per_page'	, 10);
    			$query->set('post_type'		, 'festival');
    		}
    	}
    	//return $query;
    }
    
    $posts = get_posts();
    add_action('pre_get_posts', 'my_pre_get_posts');

    Note: Code then goes on to loop through the posts.

  • The topic ‘Strange behaviour with pre_get_posts and is_main_query()’ is closed to new replies.