2

Heyho,

I've got a problem getting the post_status of a WP_PostObject which is a Woocommerce-Product. I get all products doing this query:

$onlineShopProducts = get_posts([
            'post_type' => ['product', 'product_variation'],
            'orderby' => 'post_title',
            'order' => 'ASC',
            'numberposts' => -1
        ]);

Now I want to filter out all products which are in status draft during a foreach-loop:

foreach ($onlineShopProducts as $onlineShopProduct){
            if($onlineShopProduct->post_status != 'publish'){
                continue;
            }
            doSomething($onlineShopProduct)
        }

but $onlineShopProduct->post_status and get_post_status($onlineShopProduct->ID) both return "publish", even tho they are set "Draft" in the product-edit-view... (it's in german)

enter image description here

is there some sort of post_meta I have to query or is this a know bug of some sort? How can I filter out the drafted products?

Greetings

7
  • You did not specify the post status you wanted when making the query, so no draft posts are fetched. Also note that get_posts by default will ignore caching and give a performance hit unless suppress_filters is set to false. WP_Query does not have this limitation
    – Tom J Nowell
    Commented Dec 14, 2020 at 12:30
  • Hey Tom - Thanks for your input... But problem is, that I'm getting too much, not too little... I get posts which have the draft-status in the backend as published posts in the code.
    – Husky110
    Commented Dec 15, 2020 at 15:08
  • If you only want published posts you should say so in the arguments
    – Tom J Nowell
    Commented Dec 15, 2020 at 15:33
  • Okay... Now I don't know if you don't get it cause you don't know what I'm talking about or if you even read the question... So again... There appear posts which are on "draft" in the backend but have status "published" when I view them in code... Let's say I look at post with ID 123 -> In the backend it sais status: "draft" get_post_status(123) returns "published" and all those are woocommerce-products... You get it now?
    – Husky110
    Commented Dec 15, 2020 at 15:47
  • I got it when I first read the question, and I'm saying, there's a really easy parameter you can pass to get_posts to tell it which post statuses you want, you don't need to call get_post_status. If you don't want draft posts, just say so to get_posts, the if check you're having problems with is unnecessary. Also keep in mind that WooCommerce and other 3rd party plugins are off topic, and be nice, your last comment was quite rude. Aside from a code of conduct, I'm not a support desk employee, my time is volunteered and unpaid, and there are no entitlements.
    – Tom J Nowell
    Commented Dec 15, 2020 at 15:50

2 Answers 2

1

The solution is simple, delete the if check you're having problems with, and add the post_status parameter to get_posts.

For example:

$args = [
    'post_status' => 'publish',
    'suppress_filters' => false,
];
$only_published_posts = get_posts( $args );

Notice I added suppress_filters and set it to false so that it can be sped up.

Likewise we can pass an array of the statuses we do want, e.g.

$args = [
    'post_status' => [ 'publish', 'pending' ],
    'suppress_filters' => false,
];
$only_published_and_pending_posts = get_posts( $args );

Or everything except published posts:

$args = [
    'post_status' => [ 'future', 'draft', 'pending' ],
    'suppress_filters' => false,
];
$not_published = get_posts( $args );

Or private posts:

$args = [
    'post_status' => [ 'private' ],
    'suppress_filters' => false,
];
$private_posts = get_posts( $args );

Or all post statuses, including revisions and auto drafts:

$args = [
    'post_status' => 'any',
    'suppress_filters' => false,
];
$all_posts = get_posts( $args );

There are a limited number of post statuses that come with core, and they're hard coded, so you can just list the ones you want, and omit the ones you don't.

More information at: https://developer.wordpress.org/reference/classes/wp_query/#status-parameters

Note that while this is the canonical standard WordPress method, plugins may employ custom post statuses, or ignore statuses altogether if they are poorly built. In these situations you will need to contact their dev support routes.

2

If anyone else comes here looking for this and doesn't get the answer, a Product's Variation can be set to Status Publish even when the Main Product is set to Draft. That's what was happening to me. So the product was set to draft, but the variation was set to Publish.

1
  • Hi, I ran into this too. Good catch! I'd like to improve this answer by adding instructions: to un-publish a variation uncheck the 'Enabled' checkbox within the settings for any variation.
    – Jibran
    Commented Oct 30, 2023 at 15:23

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.