1

I am trying to modify the_loop output from a plugin. I've looked into pre_get_posts, which definitely works, but I need to filter the loop by a custom taxonomy. I can do this with $query->set('tax_query',$my_tax_new_query), but the problem is this overrides any previous taxonomy queries that may have been set. It applies my query INSTEAD of the former query instead of IN ADDITION TO IT. How can I apply an additional taxonomy filter to the loop without wiping out the previous query?

EDIT:

For example:

function myplugin_filter_the_loop($query) {

    $taxonomy = 'hobbies';
    $exclude_terms = array('biking','swimming','horsebackriding');

    $query->set('tax_query',$exclude_terms)

}
add_filter('pre_get_posts','myplugin_filter_the_loop');

This works, but it will REPLACE any previously defined tax_query on the loop.

EDIT2:

Alternative using posts_where:

function myplugin_filter_the_loop($where) {

    $relationship_ids = array(97,98,99);

    foreach ( $relationship_ids as $id ) :

        $where .= " AND ( wp_posts.ID NOT IN ( SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN ($id)"

    endforeach;

}
add_filter('posts_where','myplugin_filter_the_loop');

For this to work, I'd just need to get the corresponding relationship id by term_id or slug, etc.

5
  • One alternative I've looked into is appending the posts_where filter and adding ` AND ( wp_posts.ID NOT IN ( SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN (99) ) )` where 99 refers to my taxonomy term in wp_term_relationships --- However I can't figure out how to arrive at 99 without manually coding it. Is this a good alternative? How can I get this id from a corresponding term_id or slug?
    – Matt
    Feb 21, 2014 at 16:28
  • Post your callback for the pre_get_posts hook, please. It should be a relatively simple matter of merging data.
    – s_ha_dum
    Feb 21, 2014 at 16:32
  • What is the specific context in which you need to modify the Loop? The main loop on every page, or only in certain contexts? Feb 21, 2014 at 16:45
  • Yep, every page, whether the main loop or not.
    – Matt
    Feb 21, 2014 at 16:53
  • Ok, so I ended up going with the posts_where filter in EDIT2 above. I was able to get the term ids using $term->term_taxonomy_id.
    – Matt
    Feb 21, 2014 at 22:45

1 Answer 1

2

Use $tax_query = $query->get( 'tax_query' ); to get the existing tax_query, modify it by adding your changes, then use $query->set( 'tax_query', new_$tax_query ); to put the whole thing back.

2
  • So $query->get comes back NULL, but I was able to get the original tax_query with get_query_var('tax_query'). However, now I need to wrap it in another tax_query. I haven't been able to get a nested tax_query to work. Can this be done/What is the proper format? For example, I want 'tax_query' = array('relation'=>'AND', array('taxonomy'=>'exclude', 'field'=>'id', 'terms'=>array(97, 98, 99), array('relation'='OR', array(...), array(...))
    – Matt
    Feb 21, 2014 at 17:12
  • You can't have two separate things with different relations like that. That isn't how tax_query works.
    – Otto
    Feb 21, 2014 at 20:07

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.