4

I need to adjust the main loop for all archive pages in the following way. I have registered a custom taxonomy "myposttypes". Every single post is assigned to exactly one term of "myposttypes".

If it is a category or tag archive page, only posts with "myposttypes->term1" should be included into the query result.

If it is a custom taxonomy archive page only posts with "myposttypes->term2" should be included into the query result.

I've already implemented the corresponsing pre_get_posts hook but I'm not getting any results at all for all my archive pages. I have plenty of posts with corresponding taxonomies in my blog instance so there should be results.

Here is my approach:

function wpmw_adjust_archive_queries( $query ) {
    if ( ! is_admin() && is_archive() && $query->is_main_query() ) {
        if ( is_category() || is_tag() ) $term_slug  = 'term1';
        if ( is_tax( 'beitragsarten', 'news' ) ) $term_slug = 'term2';

        $taxquery = array(
            'tax_query' => array(
                array(
                    'taxonomy' => 'myposttypes',
                    'field'    => 'slug',
                    'terms'    => $term_slug
                ),
            ),
        );

        $query->set( 'tax_query', $taxquery );
    }
}

add_action( 'pre_get_posts', 'wpmw_adjust_archive_queries' );

UPDATE: Here is my adjusted and simplified approach, that still does not work unfortunately. I get an empty result. I var_dump'ed $query and tax_query seems to be added correctly to query_vars.

function wpmw_adjust_archive_queries( $query ) {
    if ( ! is_admin() && $query->is_archive() && $query->is_main_query() ) {

        $tax_query = $query->get( 'tax_query' ) ? : array();

        $tax_query[] = array(
            'taxonomy' => 'myposttypes',
            'field'    => 'slug',
            'terms'    => array( 'term1' ),
            'operator' => 'IN'
        );

        $query->set( 'tax_query', $tax_query );
    }
}

add_action( 'pre_get_posts', 'wpmw_adjust_archive_queries' );

Here is the corresponding part of $query

[tax_query] => Array
                (
                    [0] => Array
                        (
                            [taxonomy] => myposttypes
                            [field] => slug
                            [terms] => Array
                                (
                                    [0] => term1
                                )

                            [operator] => IN
                        )

                )
  • On the category archive, should the category be ignored? – Krzysiek Dróżdż Aug 21 '18 at 9:29
  • No, the category should be considered on category archive. Basically it should be the standard loop with a kind of "filter" that only posts with myposttypes->term1 are allowed – EmWe Aug 21 '18 at 9:45
  • So be very careful not to override the default tax_query - see my answer. – Krzysiek Dróżdż Aug 21 '18 at 9:46
2

You use tax_query incorrectly. Take a look at Codex Page

tax_query should be an array which can contain:

  • relation - it should be string (AND/OR)
  • taxonomy term - array with defined taxonomy, field, terms, and so on.

In your code your setting tax_query to:

$taxquery = array(
    'post_type' => 'post',
    'tax_query' => array(
        array(
            'taxonomy' => 'myposttypes',
            'field'    => 'slug',
            'terms'    => $term_slug
        ),
     ),
);

And it should be:

$taxquery = array(
    array(
        'taxonomy' => 'myposttypes',
        'field'    => 'slug',
        'terms'    => $term_slug
    ),
);

post_type is another param, so you can't set it inside tax_query.

But... You still should be careful, if you change tax_query - it may have a value already. So if you want to be respectful of that, you should use something like this:

$tax_query = $query->get( 'tax_query' );
if ( ! is_array( $tax_query ) ) {
    $tax_query = array();
}
$taxquery[] = array(
    'taxonomy' => 'myposttypes',
    'field'    => 'slug',
    'terms'    => $term_slug
);
$query->set( 'tax_query', $taxquery );

This way you will add your tax query to already existing queries and not override them.

  • Thank you for the answer. The 'post_type' => 'post' is a copy/paste error. I deleted this line from my initial post. – EmWe Aug 21 '18 at 9:49
  • Hope it helps - and if so, remember to mark it as correct answer, so site stays tidy... – Krzysiek Dróżdż Aug 21 '18 at 9:52
  • I still have the same problem after having applied your proposed code. When I var_dump $query the is TWO times tax_query: 1.) $query->query_vars['tax_query'] 2.) $query->tax_query['queries'] Do I need to adjust both of them? – EmWe Aug 21 '18 at 10:26
  • No, that's normal - one thing is query var called tax_query and the other one is parsed tax_query object. – Krzysiek Dróżdż Aug 21 '18 at 10:27
  • 1
    Your solution is exactly what I was looking for. I found a second "pre_get_posts" hook, that was causing this error. Thank you for you help. – EmWe Aug 21 '18 at 14:04

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.