3

I have the following bit of code:

$args = array(
    'posts_per_page'   => -1,
    'category'         => 7,
    'orderby'          => 'name',
    'order'            => 'ASC',
    'post_type'        => 'product'
);

$posts = get_posts($args);var_dump($posts);

This should return one post I know that is in the category, but it isn't. If I leave out the 'category'-argument, I get all the products, so I know this should normally work. If I change the category to 1 and take out my custom post type (product), I get my default posts.

I can't see what's wrong with this. Can anyone spot what the problem is?

  • Shouldn't it be taxonomy name instead of category? – Robert hue Jun 30 '15 at 20:02
  • I tried it, but it didn't work. I got this off the codex-page of wordpress, which seems to support my approach, but still, it doesn't work: "Note: The category parameter needs to be the ID of the category, and not the category name." – Michiel Standaert Jun 30 '15 at 20:16
  • The minus1 (-1) in posts_per_page will show ALL posts and as soon you leave out the CPT wp will "fall back" at the regular posts as you already found out yourself. – Charles Jul 1 '15 at 4:49
10

In all probability you are using a custom taxonomy, and not the build-in category taxonomy. If this is the case, then the category parameters won't work. You will need a tax_query to query posts from a specific term. (Remember, get_posts uses WP_Query, so you can pass any parameter from WP_Query to get_posts)

$args = [
    'post_type' => 'product',
    'tax_query' => [
        [
            'taxonomy' => 'my_custom_taxonomy',
            'terms' => 7,
            'include_children' => false // Remove if you need posts from term 7 child terms
        ],
    ],
    // Rest of your arguments
];

ADDITIONAL RESOURCES

1
<ul>
    <?php
    $args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );

    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>
    <?php endforeach; 
    wp_reset_postdata();?>


    </ul>

May this will help You.

Thanks

  • 3
    Some explanation will be great. – Nilambar Jul 1 '15 at 3:49
  • From which 'post_type' he will get now the links, 5 in your case? Imho he wants the content of *products*(as I understand a CPT) and nothing from the regular posts. – Charles Jul 1 '15 at 4:14
  • pass your category id in arguments and from regular post you will get 5 posts. – Rohit gilbile Jul 1 '15 at 4:33
  • Read his question please, I am not always right but in this case he wants 'something' from a Custom Post Type with the name Product. – Charles Jul 1 '15 at 4:44
  • Charles is right in this case. I know how to get the data once I have my posts. The problem was that I wasn't getting the my custom posts :) – Michiel Standaert Jul 1 '15 at 8:56

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.