0

I have some products with no categories on it. Even the category 'No Category' is not attached to them. Don't know how this is happend, maybe with an import with WP All Import.

I want to get those products, but since they don't have an category I can't get them in the products overview. My luck is that there were English translations from that product which are linked to that 'ghost products' and via that way I'm able to open the product.

The second problem is that when I query on the database to get products with no category, I get no results, because they have an category but when I open the product I see there is no category checked. When saving/updating the product without no changes the product will be placed in the 'No category' category.

I want to get all those products because they must have an category. I tried to query the database to get the all the product with their categories, but as I said, all products have an category in the database.

I also tried to update all posts (wp_update_post) in a loop, but that makes no sense.

function pre($data){
    echo "<pre>";
    print_r($data);
    echo "</pre>";
}

$prods_args = array(
    'post_type' => 'product',
    'no_found_rows' => true,  // Skips SQL to count rows - a speed improvement.
    'post_status' => 'publish',
    'ignore_sticky_posts' => true,  // Don't move sticky posts to top - a speed improvement.

    'posts_per_page' => -1,
    //'paged' => $paged,
    'fields' => 'ids',  // Only return product IDs
);
$pq = new WP_Query( $prods_args );
// No need for a loop - retrieve all the IDs in one go.
$product_ids = $pq->posts;

if ( $pq->have_posts() ) {
    foreach ( $product_ids as $id ) {

        $product = get_post($id);
        //$product == wc_get_product($id);
        wp_update_post($id);
        //pre($product);
    }


}

Does anybody know how to get these 'ghost products'? I already match the data from the product and the post which I get which I print with the pre() function, but except of the product data (title, price, etc) there a no differences.

  • How can they have a category and not have a category at the same time? Your query above shows no tax query. How are you checking for categories? – Howard E Mar 12 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.