WP_Query by post ID doesn’t work if post is in custom post_status

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 );

Leave a Comment