4

I'm writing a custom plugin that is initialized at init. This plugin is trying to query for some custom post types already stored in the DB.

Here's my code:

$args = array()
$myposts = get_posts( $args );  
print_r($myposts);

No matter what arguments I pass into the $args array I don't get anything. For example:

$args = array( 'post_type' => 'page' );

Now, to my confusion if I use the exact same arguments with get_pages() I get a result.

Maybe this has something to do with when WP Query is initialized?

2
  • Is this correct from your code? $myposts = get_posts( ); i.e. you're not passing $args to get_posts()? Jan 29, 2013 at 19:23
  • @ChipBennet, Good catch! That was just a typo on my part and wasn't causing the issue. I updated the code to fix the typo. :)
    – BFTrick
    Jan 31, 2013 at 14:55

1 Answer 1

9

It seems that is was a simple problem. get_posts() has various default settings, one of which is that the post_status is set to public and my custom post type which doesn't use post_status used the default value, draft.

To fix this you can either query by post status (see the code below) or change the data in the DB.

$args = array(
    'post_status' => 'draft',
    'post_type'   => 'your_custom_post_type'
);
2
  • 4
    Wow, same problem, that was ridiculous.
    – trueinViso
    Jun 26, 2015 at 23:08
  • 1
    In my case, the problem was that by default it shows only posts of type "post", so to get custom posts you should set args array key - value pair accordingly
    – Lovor
    May 1, 2021 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.