最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

get_posts wont produce a list of custom type from a given category

programmeradmin1浏览0评论
This question already has an answer here: tax_query will produce nothing (1 answer) Closed 4 years ago.

Hi,

I created a custom post type called "news" and custom categories called "news_category" to have them categorized. I want to list "news" posts that belong to one of these categories. In this case the category id is 83. This is my code:

<?php
$news = get_posts( array(
    'post_type'      => 'news',
    'category' => 83
) );
 
if ( $news ) {
    foreach ( $news as $post ) {
        setup_postdata( $post );
        the_title();
    }
    wp_reset_postdata();
}
?>

This will produce nothing despite category ID is correct. If I use a 0 instead then it lists all posts regardless of their category but any other number will list nothing. Why is that?

By the way, I created the new taxonomy with this plugin: Custom post types

Thank you.

EDIT:

              <?php
    $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 }?>
This question already has an answer here: tax_query will produce nothing (1 answer) Closed 4 years ago.

Hi,

I created a custom post type called "news" and custom categories called "news_category" to have them categorized. I want to list "news" posts that belong to one of these categories. In this case the category id is 83. This is my code:

<?php
$news = get_posts( array(
    'post_type'      => 'news',
    'category' => 83
) );
 
if ( $news ) {
    foreach ( $news as $post ) {
        setup_postdata( $post );
        the_title();
    }
    wp_reset_postdata();
}
?>

This will produce nothing despite category ID is correct. If I use a 0 instead then it lists all posts regardless of their category but any other number will list nothing. Why is that?

By the way, I created the new taxonomy with this plugin: Custom post types

Thank you.

EDIT:

              <?php
    $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 }?>
Share Improve this question edited Aug 13, 2020 at 18:58 Cain Nuke asked Aug 11, 2020 at 19:08 Cain NukeCain Nuke 1631 silver badge7 bronze badges 3
  • It sounds like you created a custom taxonomy. If that's the case, you will need to use a taxonomy query - category is only for Core Categories. – WebElaine Commented Aug 11, 2020 at 21:34
  • how can I do that? – Cain Nuke Commented Aug 11, 2020 at 21:34
  • wordpress.stackexchange/questions/165610/… – WebElaine Commented Aug 11, 2020 at 21:35
Add a comment  | 

1 Answer 1

Reset to default 0

I'm assuming 'news_category' is a custom taxonomy; if not, and if you have simply created a new post category called 'news_category', it will only work if you add support to your custom post type. Be aware though that this will add complexities to your feeds and queries, as well as affecting how category posts show in the admin. It is most straightforward to do this with a custom taxonomy if you can.

Onto solving this using a custom taxonomy: You need to add a taxonomy query to your arguments, so WP knows where to look for that new term.

$news = get_posts(
    array(
        'post_type' => 'news',
        'tax_query' => array(
            array(
                'taxonomy' => 'nameofyourcustomtaxonomy',
                'field'    => 'slug',
                'terms'    => 'news_category',
            ),
        )
    )
);
发布评论

评论列表(0)

  1. 暂无评论