Exclude page by title for non admins

Get the page by its title, then use its ID: add_action(‘pre_get_posts’, ‘exclude_pages’); function exclude_pages($query) { if(! is_admin()) return $query; global $pagenow; if (‘edit.php’ === $pagenow && (get_query_var(‘post_type’) && ‘page’ === get_query_var(‘post_type’))) { // Get the page by its title, … $page = get_page_by_title(‘TITLE_HERE’); // then use its ID $query->set(‘post__not_in’, array($page->ID)); } return $query; } References: … Read more

Exclude post type with pre_get_posts?

I found a solution to this myself. It returns the default post type posts instead of excluding the custom post type which is fine for my needs. function my_breakfast_query ( $query ) { // not an admin page and is the main query if (!is_admin() && $query->is_main_query()){ if (is_tax( ‘food’, ‘breakfast’ )){ $tax_query = array( … Read more

Sort WordPress Archive by multiple oderby arguments in pre_get_posts action

I am not an expert, but you have a couple of issues here Firstly, is_archive() should be an object of $query. You should also just target the front end with your function. Remember, pre_get_posts alters all instances of WP_Query, front end and backend. So this line if(is_archive() && $query->is_main_query()) : should look something like this … Read more

Why query by specific date with variables doesn’t return same result that with harcoded integers?

I believe your problem is that you are not casting the variables over into integers. You should give this a try: $query->set(‘date_query’, array( array( ‘year’ => (int)$dies[‘year’], ‘month’ => (int)$dies[‘mon’], ‘day’ => (int)$dies[‘mday’], ), )); or if you would like, before you even set up the arguments for the query, you could do something along … Read more

Altering the main query using get_post_meta() in pre_get_posts

Per the wordpress codex pre_get_posts() does not work everywhere. is_font_page() will not work but is_home() will. So your condition is_front_page() && is_home() will fail every time. However ‘is_home()’ alone should work. It might be helpful to others to know what exactly you are trying to do. Usually pre_get_posts is used to alter a query but … Read more

pre_get_posts with multiple queries

You need to have two queries, one handling the listings, the other handling the map. Since you need pagination for the listings, I’d suggest you use pre_get_posts for that query, so that you can use WordPress’ default pagination out of the box. For the map, create a new WP_Query in your archive template: $args = … Read more

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

Override tax_query with pre_get_posts to include other term_ids on a single category

but now it seems, I have two tax_queries, one is in WP_Query->query_vars and the other is just in WP_Query That is normal. WP_Query::$query_vars is an array of query vars merged with the default ones for WP_Query. So if you do $query = new WP_Query( array( ‘post_status’ => ‘publish’ ) ) (or $query = new WP_Query( … Read more