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
- Adding a Custom Post Type into the menu screen
- Highlighting wp_nav_menu() Ancestor Class w/o Children in Nav Structure?
- Media library – Limit images to custom post type
- Remove current_page_parent nav class from blog index when in CPT
- Custom sortable columns ordered by meta-value?
- How can I filter posts by post_parent in the admin?
- Now can I group custom post types together?
- Filter for “get_post_type_archive_link()”
- Filter by custom Field for Custom post type Admin Listing
- wp_list_pages not showing cpt as I expected
- Remove wpautop from all posts/pages except my custom post type
- Remove date and category filters when editing custom post types
- Taxonomy terms with edit/filter link in wp-admin, in the list of custom posts
- Genesis Child and Custom Post Type
- Using Custom Templates for Custom Post Types for the Genesis Theme Framework?
- wp_query and comment_parent – select only posts with top level comments
- Want to filter only parent post in admin area
- Which User Role for Custom Post Type Menu – wp_nav
- Create Custom Post Type Archive Page with Sub Categories Navigation Sidebar
- Filters post in admin with dropdown select, custom post type
- Admin List Dynamic Heading
- How can I add a column in the wp_list_table of the admin area?
- How to filter the description of a custom post type
- Remove current_page_parent from posts page link in WordPress nav menu
- Why are navigation items saved in the database?
- Filter home_url for custom post type
- Add Content Exclusively to a Custom Post Type Feed
- ‘Pages’ widget alternative for custom post types?
- Including Custom Post Type posts in a page template contextually (or should I widget?)
- Add the current menu item CSS class to a custom page type archive in WordPress menu
- How to change the admin menu “Pages” to something else
- Navigation menus not showing because of custom post type filter
- register_taxonomy and register_post_type does not work [closed]
- Why is conditionally loading a custom plugin’s code only on a specific custom post type causing the site content to disappear?
- How to query custom post types posts filtered by multiple custom taxonomies through a form selection
- Function to display custom post type on front page makes menu items dissapear
- Using default WP menu functionality to link to custom post-type listing?
- A method for ordering mixed dates in search result loop (theory only, no actual code)
- How to change the post type a theme shows by default?
- filter custom post in rest api with custom function
- How to display custom WP menus?
- Filter hierarchical custom post type admin page by parent, and include children & grandchildren
- Sort and filter custom post type posts by custom taxonomy
- Versioned Custom Post Type (Not the same as revisions)
- the_content filter on some post types only not working
- Is there a way to establish parent/child relationship in WP menu links exclusively?
- Custom filter function not working with Custom post type
- A page that shows a list of a specific custom post type
- Search filter triggered & sort by custom post type
- Redirect to another page using contact form 7? [closed]
- How can I add a filter to a particular post format?
- wordpress remove views from action links in a custom post
- Including link to custom post type in ‘wp_list_pages’ function
- Add an Outside Wrapper to my Header and Make it Full Width
- How can I remove filters from custom post types?
- WordPress menu with custom taxonomy
- Query to get child pages of current page and display it in action hook
- Problem with Apply Filters on URL in meta box filed of custom port type
- how to retain the ability to modify the post slug after applying a post_type_link filter?
- Custom Sidebar in Editor (not Widget) for Custom Post Type in Genesis
- Change Custom Post Type singular_name through function/filter
- highlight parent page menu item when in custom post type
- show most viewed posts of last days by link?
- Custom post type post in custom menu
- Home page is getting current-menu-item even if it’s not home
- conditional filter
- Best way to fix bad count on All | Mine | Published
- Filter in Custom post type to find the parent post
- 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
- add_rewrite_tag broke permalinks that doesn’t use that specific tag
- pricefilter without WooCommerce
- 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
- 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?
- how to display notifications in the wordpress menu when a new post is published
- Highlight a Post on archive page if it has a new comment?
- Custom post type with Filter and Page Nav
- Converting a checkbox filter for custom fields to a dropdown
- Exclude Custom Post Type from shared Custom Taxonomy
- Diffrent search templates for different post types
- How to filter posts by categories?
- Getting (Invalid) when adding custom taxonomy of custom post type link in menu [closed]
- current_page_item and custom post type?
- 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
- How to display custom message for (Genesis) featured posts if no posts
- sidebar hierarchical menu category & custom post type
- Trying to set up a range filter for related custom post types
- When Attempting to Filter Plugin Generated Content Using Filter post_type_link, Permalinks Are Not Modified
- Change CPT Edit Target Link for Admin List
- Can’t add custom post type archive page to menu
- Add Custom Taxonomy Terms as CSS Classes for CPT Posts in an Elementor Loop Item Template
- coloring row’s background based on custom field value