TransWikia.com

tax_query will produce nothing

WordPress Development Asked by Cain Nuke on February 25, 2021

I want to display custom type posts from a custom taxonomy category so I did this:

$news = get_posts(
array(
    'posts_per_page' => -1,
    'post_type' => 'news',
    'tax_query' => array(
            'taxonomy' => 'news_category',
            'field' => 'term_id',
            'terms' => 82,
        ),
    )
); 
$postslist = get_posts( $news );    

foreach ($postslist as $crap) { ?>
     <a href="<?php $crap->title; ?>"><?php $crap->title; ?></a>
<?php }?>

I get nothing from this. Why is that? I double checked the category ID and its correct. Also, I used the Custom post types plugin to register the custom taxonomy.

One Answer

Looking at the WP_Query documentation on tax_queries we can see it accepts a nested array as a parameter. Compare that to the provided code:

'tax_query' => array(
    'taxonomy' => 'news_category',
    'field' => 'term_id',
    'terms' => 82,
),

Should look like:

'tax_query' => [
    [
        'taxonomy' => 'news_category',
        'field' => 'term_id',
        'terms' => 82,
    ]
],

Answered by Howdy_McGee on February 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP