8

I am running into one issue with pre_get_post filter.

Actually I have 4 post types. Post, Pages, Events, and Venue. I created one custom field called city for event and venue post type.

When creating custom field I have one option to select post type on which I want to display that custom field.

Now, on the front end part I added one filter on pre_get_posts and modified the main query to get all posts from specific city.

All things are working perfectly on events and venue listing and single page but this filter also applies on post and pages as well.

What I want is, **I want to remove the this filter from the post type which are not included with that custom field.

I have tried with $wp_query->query_vars['page_id'] this code but its not working any more.

Can any one help me to sort this our ?

FYI: My code looks like,

add_action( 'pre_get_posts', 'citywise_post_filter' );
function citywise_post_filter($query) {
    global $wp_query;
    if( isset( $_REQUEST['city_filter'] ) ){
        $city = $_REQUEST['city_filter'];
    }else{
        $city = $_SESSION['CURR_CITY'];
    }
    if ( !is_admin() ){
        $query->set( 'meta_key', '_ct_selectbox_52f65ae267764' );
        $query->set( 'meta_value', $city );
        return;
    }
}

Thanks, Aezaz

2 Answers 2

21

Improve your conditional to include a check for post type being queried. It can be done via WP_Query::get method

So, where you have

if ( !is_admin() ){
    $query->set( 'meta_key', '_ct_selectbox_52f65ae267764' );
    $query->set( 'meta_value', $city );
    return;
} 

replace with

if ( ! is_admin() && in_array ( $query->get('post_type'), array('event','venue') ) ) {
    $query->set( 'meta_key', '_ct_selectbox_52f65ae267764' );
    $query->set( 'meta_value', $city );
    return;
}

Also note that using this code, as @offroff noted in his answer, the filter will apply to all queries, the ones you triggered visiting a page (main query) and the ones being triggered via code, using new WP_Query ($args) (secondary queries).

To make the code filter not affect secondary queries, add a check for $query->is_main_query(), so the code becomes:

if ( is_admin() || ! $query->is_main_query() ) return;

if ( in_array ( $query->get('post_type'), array('event','venue') ) ) {
    $query->set( 'meta_key', '_ct_selectbox_52f65ae267764' );
    $query->set( 'meta_value', $city );
    return;
}
3
  • Hi G.M. Thanks a lot. It works. only on single detail page. I can't able to get the current post type in category listing pages with $query->get('post_type') and get_post_type(). Commented Feb 16, 2014 at 7:42
  • @ShaikhAezaz if in the category listing pages you want mix event, venue and standard post types, you probably can't do the trick using a pre_get_post filter. If the taxonomy shows only event and/or venue (and no other post types) you can use is_tax('your_taxonomy') to add another filter. Your aim is not very clear, maybe you can add more details in the question.
    – gmazzap
    Commented Feb 16, 2014 at 17:38
  • G.M. Yes I have other custom post types as well. And I just added city meta field to event and place but not in other posts. So I just want the filter that returns current post type . Commented Feb 17, 2014 at 17:48
4

Wordpress has a great page about conditional tags

Along with !is_admin() you might wanna do && $query->is_main_query()

In the top of your function you can do if(is_single() || is_page()) {return;} if you don't want the filter to apply on those pages.

Other conditionals u might be able to use is is_post_type_archive('event') && is_post_type_archive('venue')

Edit: Not sure I understand your problem but now I figured this might help you:

$query->set('post_type',array('event','venue'));
2
  • Hi offroff, I used the code that G.M. suggested and it works for me for category and taxonomy listing pages but now I can't able to get the post type on category and taxonomy listing pages. You know some solution with this ? Commented Feb 16, 2014 at 9:47
  • Maybe you have to set the post type, for example if(is_category()) {$query->set('post_type',array('event','venue')); }. It's hard to know how to solve your problem because we don't know what templates you're using.
    – offroff
    Commented Feb 16, 2014 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.