pre_get_posts
filters will be applied to all queries on a page- the main query, menus, and additional WP_Query instances.
To target only the main query, use is_main_query()
:
function my_pre_get_posts( $query ) {
// bail early if is in admin
// or if query is not main query
if( is_admin() || !$query->is_main_query() ) {
return;
}
// get meta query
$meta_query = $query->get('meta_query');
if(!empty($_GET['contract']))
{
$contract = explode(',', $_GET['contract']);
$meta_query[] = array(
'key' => 'contract',
'value' => $_GET['contract'],
'compare' => 'IN',
);
}
$query->set('meta_query', $meta_query);
return;
}
Related Posts:
- add current-menu-item to multiple custom post types
- Custom Permalink For Custom Post Type – Working Fine But Broken For Pagination
- Query filter by value in meta_value array
- Best way to fix bad count on All | Mine | Published
- How we can create menu from Appearance > Menu and use as a add-submenu-page for admin menu?
- WP Admin Dropdown List Filter for custom (ACF) field on custom post type(s)
- Is there any filter to add a custom attribute to the tag in admin panel?
- build child and anchestor three from post parent
- Display category filters for custom post type when category is shared by multiple post types
- Store a value in global scope after init hook is fired
- Filter in Custom post type to find the parent post
- wordpress get menu id by custom post_type?
- Dynamic page for nav items used as filters
- cpt not display inside nav menu
- Create Post Types from a XML url (Real Estate website)
- Filter posts by their related field’s custom field
- Post Click Redirect to Custom URL instead of Single Post Page
- How to create a Child & Siblings menu for a custom post type?
- wp_get_nav_menu_items doesn’t return custom post type item
- How can I add image sizes for a specific custom post type?
- Add content to wordpress edit.php page
- How to use multiple orderby conditions on query search results? (orderby one post type, relevance)
- add_rewrite_tag broke permalinks that doesn’t use that specific tag
- pricefilter without WooCommerce
- Output all terms slugs for a loop filter
- Insert custom taxonomy into category query
- filter rest api post by a acf filed
- Group Custom Posts Types in a Relation field of ACF
- Automatically add CPT UI categories to the menu
- Help Adding filter to Add Media button for custom post type
- Custom post type category link + add to menu
- Apply pre_get_posts to specific custom post type in the admin area
- Possition a Custom Post Type Tab under Settings
- how it’s possible to show from a post of a custom post type the taxonomy/terms?
- Keep br tags from stripping
- Navigation not working for custom post type pages
- wp_nav_menu doesn’t seem to work on custom post type pages
- Add filter button to custom post type in admin area
- Enable shortcodes on custom post type
- Custom Pagination based on Custom Post Type
- Filter between Custom Posts depending on meta_value
- how to display notifications in the wordpress menu when a new post is published
- Grouping and paging CPT events by month with custom field date
- Highlight a Post on archive page if it has a new comment?
- Creating adminable dynamic filtering on custom post type
- Custom nav menu current item custom link problem
- Adding Information To All Posts Screen
- Dropdown switching subcategories portfolio
- Filter Content on all Post Types
- Use the page picker from wordpress menu creation page
- Filter wp_dropdown_categories Per Post Type
- Custom post type with Filter and Page Nav
- How to display custom post type in a submenu?
- Different read-more link for each custom post type [closed]
- Custom Post Type parent slug as menu item
- current_menu_parent for custom post type and custom url
- Sorting on Taxonomy in archive page
- Calling the first & last post by category in custom post type
- Converting a checkbox filter for custom fields to a dropdown
- Filter custom post type by tags
- List all posts from custom post type by taxonomy
- Nav style for current page with custom post type
- Admin menu post type
- Custom Search Template for Custom Post Types
- Exclude Custom Post Type from shared Custom Taxonomy
- using ACF datepicker to filter posts on a page
- How to display the FAQ Archive page in my menu
- Hook only specific post type
- how to override menu widget?
- Restrict a filter to a custom post type. Am I doing this right?
- Custom Taxonomy Showing in WP Menu
- Diffrent search templates for different post types
- Plugins for putting Custom Post Types into the Menu
- No Current Class showing in Menu for Custom Post Types
- Custom post type in submenu with custom meta box as a title with custom html
- CPT Archive with parent page
- How to filter posts by categories?
- WordPress URL Problems and Layout
- Getting (Invalid) when adding custom taxonomy of custom post type link in menu [closed]
- Setting a Master Menu on a Custom Post Type
- Show posts of parent in edit.php
- current_page_item and custom post type?
- and custom post_types to custom menu
- Menu that shows months and filters posts to show only posts from that month and shows posts as sub items
- How to make a single Menu Item call another Mega Menu for Custom Post Types and Custom Taxonomies?
- Highlight specific menu item when custom post is page
- How do I filter a custom post type loop by a field?
- A to Z list for custom post types
- Rewrite a filter as shortcode (or something like that) to use anywhere in CPT
- add_filter > posts_where works partially
- Simple Share Buttons Add Plugin and Custom Post Type
- How to display custom message for (Genesis) featured posts if no posts
- sidebar hierarchical menu category & custom post type
- Navigation won’t update to show full path to single post
- Can WordPress show posts based on a button that the user clicked 2 pages back?
- Trying to set up a range filter for related custom post types
- Menu Structure and URL structure, with Pages and Post-Type-Posts
- Filter page ID outside the loop and order
- When Attempting to Filter Plugin Generated Content Using Filter post_type_link, Permalinks Are Not Modified
- Paginate yearly archives for a custom post type