Reputation: 107
I am using get_posts($query) to list posts in my WordPress plugin.
If I use it like this:
$query['post_type'] = array('post', 'attachment');
Then it will not return 'attachment' types, only posts. They will be returned only if I query for them separately, like this:
$query['post_type'] = 'attachment';
I am using WordPress 4.9.6.
What am I missing here?
Thank you.
Upvotes: 2
Views: 1880
Reputation:
The problem is attachments have post_status == 'inherit' not 'publish' so I would try adding
$query['post_status'] = [ 'publish', 'inherit' ];
to $args.
Note that this may get more results then intended as post_type == 'post' with post_status == 'inherit' will also be returned. You should probably do two separate queries. This is just an explanation as to why your combined query does not work as expected.
The reason
$query['post_type'] = 'attachment';
works is the WordPress has the following code in get_posts()
if ( empty( $r['post_status'] ) )
$r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
}
Upvotes: 3
Reputation: 1980
Can you try
$args = array(
'post_type' => array('post', 'attachment')
);
$multi_posts = get_posts( $args );
Make sure to check the name of your custom post type.
Upvotes: 1