Menu disappears when meta_key changed [closed]

if ( $query->is_main_query() && $query->is_singular ) { return; } This is the wrong way around – it’s actually ensuring that the query is being modified on everything except the main query, which is why it’s affecting the menus (which are also built using queries). You need to change it to if ( !$query->is_main_query() && !$query->is_singular …

Read more

pre_get_posts having no effect on my category archive

You’ve got a typo there, should be: is_admin(). You should enable debugging by setting define(‘WP_DEBUG’, true); in wp-config.php to see such errors. Also there’s no need of returning anything in action hook. add_action( ‘pre_get_posts’,’filter_archive’, 1 ); function filter_archive( $query ) { if ( ! is_admin() && is_category(’76’) && $query->is_main_query() ) $query->set(‘posts_per_page’, ’12’); } Sidenote: personally …

Read more

Another query in pre_get_post cause memory issue

Remove the action before you run the internal query, and always verify main query with is_main_query: function custom_loops($query) { if ( $query->is_tax( ‘service_photo_location’ ) && $query->is_main_query() ){ remove_action( ‘pre_get_posts’, ‘custom_loops’ ); $ids = get_nearby_partners_by_area(50000, ‘service_photo’); // … } } add_action(‘pre_get_posts’, ‘custom_loops’);

pre_get_posts variables

The logic inside the is_home function relies on the main query, but by definition that query hasn’t happened yet as you’re still in the pre_get_posts filter. Instead, consider $q->is_home(). The same goes for is_feed() $q->is_feed()

Automatically applying a pre_get_posts filter for child categories only

Try this: EDIT: added short circuit return statements to avoid running unnecessary code on admin pages or other queries (e.g. nav menu items) // Custom query for research sub-categories function custom_query_for_research_subcategories($query) { if (is_admin() || !$query->is_main_query()) { return $query; } $qobj = get_queried_object(); if (isset($qobj->taxonomy) && ‘category’ == $qobj->taxonomy ) { $term = get_term($qobj->term_id); if …

Read more

how to restrict posts_request filter to the main query only

The posts_request filter actually takes a second argument, which is the query. You can check if that query is the main query. Try this: add_filter( ‘posts_request’, ‘my_request_filter’, 10, 2 ); function my_request_filter($sql, $query) { if($query->is_main_query() && is_search()) { $sql=”some custom sql query” } return $sql; }