5

I want to display a single post by its ID using WP_Query(). To do this, I use the following argument:

$args = array(
   'p' => 61202);

That should work, but does not seem to work if the post is in a custom post_status.

To fix this, I tried the following:

$args = array(
   'p' => 61202, // id of post
   'post_type' => 'any', // apply to all post types
   'post_status' => 'my_custom_status' // the custom post status
);

But this still does not work. Why?!

Is there a known bug? I am running this code in the wp-admin area. It works fine if the post is in any of the WP core post statuses.

4

WP_Query has a bug (#29167) when you try to fetch posts with (almost?) any other status than publish. This bug seems to be fixed in trunk. I haven’t tested it, so I cannot tell if it covers your use case. Try it, and give feedback there if it doesn’t.

There are two workarounds:

  1. Use get_post( $post_id ). This runs its own query and isn’t affected by this bug.

  2. Use a custom DB query if you don’t know the post IDs:

    $sql      = "SELECT * FROM $wpdb->posts WHERE post_status = %s AND post_type = %s";
    $prepared = $wpdb->prepare( $sql, $status, $post_type );
    $posts    = $wpdb->get_results( $sql );
  • 1
    I can confirm that the patch posted in the bug report does indeed work. – Christine Cooper Feb 4 '15 at 2:34

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.