0

I have two machines: local and deployment.

On my local machine I have the code:

$all_prods = array(
    'posts_per_page' => -1,
    'post_type'      => 'product',
);

dump(query_posts($all_prods));

The result I'm getting is my three products, that I've created earlier. But the same code on deployment machine returns an empty array despite of the website has many products in it.

I thought that somehow deployment machine has the other prefix for it's post type, than my local one, but when I type on deployment machine:

$prod = wc_get_product(440);
dump($prod);

I get WC_Simple_Product object with field "post_type" equal to "product". How can I debug it?

  • do you have the same plugins with same configurations on the 2 websites ? – mmm Nov 18 '17 at 10:26
3

First I would state that in most cases the query_posts should be avoided!

This function will completely override the main query and isn’t intended for use by plugins or themes. Its overly-simplistic approach to modifying the main query can be problematic and should be avoided wherever possible.

I would suggest transfer your query with WP_Query and try something like this:

$all_prods = array(
    'posts_per_page' => -1,
    'post_type'      => 'product',
);
$query = new WP_Query($all_prods);

while ($query->have_posts()) {
    $query->the_post();
    $post_id = get_the_ID();
    echo $post_id;
}

For the Debug part, I would use the get_post($post_id); to verify that we talk about the same post_type.

  • Thank you, for trying to help me. I've tried it with new WP_Query too but have_posts() returned false. I think, I,ve finally found where the bug was. – Liam Kernighan Nov 18 '17 at 10:33
  • 1) Do you have any Loop/ WP_query before this? 2) IS any plugin overriding the WP_Query ? – Drupalizeme Nov 18 '17 at 10:54
0

I've found the solution.

$query = new WP_Query();
$all_prods = array(
    'posts_per_page' => -1,
    'post_type'      => 'product',
);
$query = new WP_Query($all_prods);
echo $query->request . '<BR>';

The output was different on 2 machines, there was a problem with a multilanguage plugin that automatically added a second table and made inner join with it.

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.