Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Is this correct from your code? $myposts = get_posts( ); i.e. you're not passing $args to get_posts()? –  Chip Bennett Jan 29 '13 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 '13 at 14:55
add comment

2 Answers

up vote 1 down vote accepted

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'
);
share|improve this answer
add comment

get_posts() is more to be used within The Loop.

but to get that working outside the loop, try putting global $post; first at the top...

global $post;
$args = array( 'post_type' => 'page' );
$myposts = get_posts($args);
share|improve this answer
    
Good guess, but it didn't seem to do anything. –  BFTrick Jan 29 '13 at 18:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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