1

I am building a custom WordPress function/plugin that uses get_posts. The following code returns an empy array. I have included wp-load.php in my script. Is there something else I am missing?

$args = array(
    "posts_per_page"   => -1,
    "paged"            => 0,
    "orderby"          => "post_date",
    "order"            => "DESC",
    "post_type"        => "carellcars, ccf, attachment",
    "post_status"      => "publish, inherit",
    "post_author"       => "2"

);

$posts_array = get_posts($args); 

print_r($posts_array);

die;

If I use arrays for post_type and _status the array is not populated, but if I use single values for post_type and _status the array is populated?

Not populated:

$args2 = array(
    'posts_per_page'   => -1,
    'paged'            => 0,
    'orderby'          => 'post_date',
    'order'            => 'DESC',
    'post_type' => array('carellcars', 'ccf', 'attachment'),
    'post_status'      => array('publish', 'inherit')
);

Populated:

$args2 = array(
    'posts_per_page'   => -1,
    'paged'            => 0,
    'orderby'          => 'post_date',
    'order'            => 'DESC',
    'post_type' =>  'carellcars',
    'post_status'      => 'publish'
);

Update, removing "ccf" post type produces results, but I do have a ccf post_type and would like to include them. Any ideas?

    'post_type' => array('carellcars', 'attachment'),
    'post_status'      => array('publish', 'inherit')
18
  • Try removing meta_key and meta_value entirely. What you're asking for here is for all posts that have an empty meta key with an empty meta value actually saved.
    – WebElaine
    Commented May 9, 2019 at 16:06
  • If you're building a plugin, why are you including wp-load,php? Is this not running inside of WP somewhere?
    – WebElaine
    Commented May 9, 2019 at 18:36
  • 4
    Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under the CC BY-SA 3.0 license for SE to distribute that content. By SE policy, any vandalism will be reverted. If you want to know more about deleting a post, consider taking a look at: How does deleting work?
    – iBooot
    Commented May 10, 2019 at 16:34
  • 1
    You should edit the details in your answer into the question itself, or ask a new one if the answers did not suffice
    – Machavity
    Commented May 10, 2019 at 17:50
  • 1
    No, query works with single post type 'carellcars', try with array('carellcars'), next with array('carellcars', 'page') or array('carellcars', 'post'). As I wrote earlier, when the post type is an array, I can see the results. I do not know why it is different with you. The trial and error method remains.
    – nmr
    Commented May 10, 2019 at 18:20

1 Answer 1

2

Try

"post_type" => array('carellcars', 'ccf', 'attachment'),
0

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.