1

I have two custom taxonomy 'cities-taxonomy' and 'userfeed-taxonomy' and I am excluding all the post which has these taxonomies assigned in my blog page using pre_get_post();

Here is my query

add_action( 'pre_get_posts', 'my_filter_main_query' );

function my_filter_main_query( $query ) {

if ( $query->is_home() && $query->is_main_query() ) {

    $terms1 = get_terms( array( 'userfeed-taxonomy' ), array( 'fields' => 'ids' ) );
    $terms2 = get_terms( array( 'cities-taxonomy' ), array( 'fields' => 'ids' ) );


    $query->set( 'tax_query', array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'userfeed-taxonomy',
            'field' => 'id',
            'terms' => $terms1,
            'operator' => 'NOT IN'
        ),
        array(
            'taxonomy' => 'cities-taxonomy',
            'field' => 'id',
            'terms' => $terms2,
            'operator' => 'NOT IN'
        ),
    ));
}}

Now what I want is that if

featured

category is assigned in addition to above taxonomies that post must not be excluded from query.

Any help appreciated, may be some other method I can alter the blog page query to achieve this ?

2

I haven't run this code so there may be a syntax error, but the gist is to return userfeed-taxonomy with the terms specified that have featured category OR cities-taxonomy with the terms specified and featured category OR anything else that's not those taxonomies. It's kind of clunky, maybe there is a better way:

    $query->set( 'tax_query', array(
        'relation' => 'OR',
        array(
            array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'userfeed-taxonomy',
                    'field' => 'id',
                    'terms' => $terms1
                ),
                array(
                    'taxonomy' => 'category',
                    'field' => 'slug',
                    'terms' => array( 'featured' )
                ),
            ),
        ),
        array(
            array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'cities-taxonomy',
                    'field' => 'id',
                    'terms' => $terms2
                ),
                array(
                    'taxonomy' => 'category',
                    'field' => 'slug',
                    'terms' => array( 'featured' )
                ),
            ),
        ),
        array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'userfeed-taxonomy',
                'field' => 'id',
                'terms' => $terms1,
                'operator' => 'NOT IN'
            ),
            array(
                'taxonomy' => 'cities-taxonomy',
                'field' => 'id',
                'terms' => $terms2,
                'operator' => 'NOT IN'
            )
        )
    ) );
  • Thanks a nice try,I have edited your answer to make it work for me. – abhishekfdd May 26 '15 at 5:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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