Quantcast
Channel: Include and Exclude Taxonomies From Archives & Feeds Using 'pre_get_posts' - WordPress Development Stack Exchange
Viewing all articles
Browse latest Browse all 3

Include and Exclude Taxonomies From Archives & Feeds Using 'pre_get_posts'

$
0
0

What I am trying to do?

My blog uses a custom taxonomy called edition with terms like us-canada (6), eu (7) and india (8) -- term slug (ID).

I want to make sure that posts not assigned to any specific 'edition' be shown under all the terms (i.e. if a post is not assigned to usa, europe or india, it'll be shown in the archive pages of all these terms).

What I've tried?

Here's the sample code pertaining to one of the terms, which should give you an idea of what I am exactly trying to do, and what I'd be doing wrong.

add_filter('pre_get_posts','better_editions_archive');function better_editions_archive($query) {    if ( $query->is_tax( 'edition', 6 ) && $query->is_main_query() ) {        $query->set( 'post_type', array( 'post' ) );        $query->set( 'tax_query',            array('relation' => 'AND',                array('taxonomy' => 'category','field' => 'id','terms' => array( 1, 2, 4, 5 )                ),                array('taxonomy' => 'edition','field' => 'id','terms' => array( 7, 8 ),'operator' => 'NOT IN'                )            )        );    }    return $query;}

What's wrong? The aforementioned code doesn't work (others I've tried: code-1, code-2), it's doesn't change anything. There's no debug error either.

So, what could I be doing wrong?

ALSO, to make sure that the changes also apply to feeds of these terms, I replaced the relevant line in the aforementioned code with this:

function better_editions_archive($query) {    if ( ( $query->is_tax( 'edition', 6 ) && $query->is_main_query() ) || ( $query->is_feed() && $query->is_tax( 'edition', 6 ) ) ) {

But that starts to redirect the terms' feeds back to the terms' archives. That is, with the function in place, example.com/edition/usa/feed/ redirects back to example.com/edition/usa/.

Again, I've no clue as to what I'd be doing wrong.

UPDATE: What's worked for me? (But...)

add_filter( 'pre_get_posts', 'better_editions_archive' );function better_editions_archive( $query ) {    if ( $query->is_tax( 'edition', 6 ) && $query->is_main_query() ) {        $args = array('post_type' => 'post','tax_query' => array('relation' => 'AND',                array('taxonomy' => 'category','field' => 'id','terms' => array( 1, 2, 4, 5 )                ),                array('taxonomy' => 'edition','field' => 'id','terms' => array( 7, 8 ),'operator' => 'NOT IN'                )            )        );        $query->query_vars = $args;    }    return $query;}

It works, but the problem is, I'd an extended chat discussion with a knowledgeable WordPress developer, and he told me this (you can follow the full conversation here, but it's too lengthy):

And you're then assigning this array as the query_vars. Now the query_vars is a pretty large object. And you're basically overwriting the data in there and just adding your custom ones. This means that you 'unset' everything that is added per default.

He pretty much advised against using this solution, and instead go with the $query->set(); method.

But as you can see far above, I couldn't get the other one to work. So I am here to see if anyone can tell me what I am doing wrong, in a less-technical-speak.


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images