0

I'm writing a plugin for WooCommerce. somewhere in my plugin I check that a query has post or not, but all the time myWPQUERYobject->have_posts() returns false! here is var_dump of the object:

object(WP_Query)#17793 (48) { ["query"]=> array(8) { ["post_type"]=> string(10) "shop_order" ["post__in"]=> array(21) { [0]=> string(5) "12501" [1]=> string(5) "12502" [2]=> string(5) "12503" [3]=> string(5) "12506" [4]=> string(5) "12510" [5]=> string(5) "12513" [6]=> string(5) "12528" [7]=> string(5) "12529" [8]=> string(5) "12536" [9]=> string(5) "12557" [10]=> string(5) "12558" [11]=> string(5) "12561" [12]=> string(5) "12565" [13]=> string(5) "12575" [14]=> string(5) "12576" [15]=> string(5) "12578" [16]=> string(5) "12587" [17]=> string(5) "12601" [18]=> string(5) "12602" [19]=> string(5) "12603" [20]=> string(5) "12604" } ["post_status"]=> string(7) "publish" ["posts_per_page"]=> int(20) ["order"]=> string(4) "DESC" ["meta_key"]=> string(14) "_customer_user" ["meta_value"]=> int(10) ["tax_query"]=> array(1) { [0]=> array(3) { ["taxonomy"]=> string(17) "shop_order_status" ["field"]=> string(4) "slug" ["terms"]=> array(1) { [0]=> string(9) "Completed" } } } } ["query_vars"]=> array(66) { ["post_type"]=> string(10) "shop_order" ["post__in"]=> array(21) { [0]=> string(5) "12501" [1]=> string(5) "12502" [2]=> string(5) "12503" [3]=> string(5) "12506" [4]=> string(5) "12510" [5]=> string(5) "12513" [6]=> string(5) "12528" [7]=> string(5) "12529" [8]=> string(5) "12536" [9]=> string(5) "12557" [10]=> string(5) "12558" [11]=> string(5) "12561" [12]=> string(5) "12565" [13]=> string(5) "12575" [14]=> string(5) "12576" [15]=> string(5) "12578" [16]=> string(5) "12587" [17]=> string(5) "12601" [18]=> string(5) "12602" [19]=> string(5) "12603" [20]=> string(5) "12604" } ["post_status"]=> string(12) "wc-Completed" ["posts_per_page"]=> int(20) ["order"]=> string(4) "DESC" ["meta_key"]=> string(14) "_customer_user" ["meta_value"]=> int(10) ["tax_query"]=> array(0) { } ["error"]=> string(0) "" ["m"]=> string(0) "" ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost"]=> string(0) "" ["subpost_id"]=> string(0) "" ["attachment"]=> string(0) "" ["attachment_id"]=> int(0) ["name"]=> string(0) "" ["static"]=> string(0) "" ["pagename"]=> string(0) "" ["page_id"]=> int(0) ["second"]=> string(0) "" ["minute"]=> string(0) "" ["hour"]=> string(0) "" ["day"]=> int(0) ["monthnum"]=> int(0) ["year"]=> int(0) ["w"]=> int(0) ["category_name"]=> string(0) "" ["tag"]=> string(0) "" ["cat"]=> string(0) "" ["tag_id"]=> string(0) "" ["author"]=> string(0) "" ["author_name"]=> string(0) "" ["feed"]=> string(0) "" ["tb"]=> string(0) "" ["paged"]=> int(0) ["preview"]=> string(0) "" ["s"]=> string(0) "" ["sentence"]=> string(0) "" ["title"]=> string(0) "" ["fields"]=> string(0) "" ["menu_order"]=> string(0) "" ["embed"]=> string(0) "" ["category__in"]=> array(0) { } ["category__not_in"]=> array(0) { } ["category__and"]=> array(0) { } ["post__not_in"]=> array(0) { } ["post_name__in"]=> array(0) { } ["tag__in"]=> array(0) { } ["tag__not_in"]=> array(0) { } ["tag__and"]=> array(0) { } ["tag_slug__in"]=> array(0) { } ["tag_slug__and"]=> array(0) { } ["post_parent__in"]=> array(0) { } ["post_parent__not_in"]=> array(0) { } ["author__in"]=> array(0) { } ["author__not_in"]=> array(0) { } ["ignore_sticky_posts"]=> bool(false) ["suppress_filters"]=> bool(false) ["cache_results"]=> bool(true) ["update_post_term_cache"]=> bool(true) ["lazy_load_term_meta"]=> bool(true) ["update_post_meta_cache"]=> bool(true) ["nopaging"]=> bool(false) ["comments_per_page"]=> string(2) "50" ["no_found_rows"]=> bool(false) } ["tax_query"]=> object(WP_Tax_Query)#17790 (6) { ["queries"]=> array(0) { } ["relation"]=> string(3) "AND" ["table_aliases":protected]=> array(0) { } ["queried_terms"]=> array(0) { } ["primary_table"]=> string(11) "mdvat_posts" ["primary_id_column"]=> string(2) "ID" } ["meta_query"]=> object(WP_Meta_Query)#17791 (9) { ["queries"]=> array(2) { [0]=> array(2) { ["key"]=> string(14) "_customer_user" ["value"]=> int(10) } ["relation"]=> string(2) "OR" } ["relation"]=> string(3) "AND" ["meta_table"]=> string(14) "mdvat_postmeta" ["meta_id_column"]=> string(7) "post_id" ["primary_table"]=> string(11) "mdvat_posts" ["primary_id_column"]=> string(2) "ID" ["table_aliases":protected]=> array(1) { [0]=> string(14) "mdvat_postmeta" } ["clauses":protected]=> array(1) { ["mdvat_postmeta"]=> array(5) { ["key"]=> string(14) "_customer_user" ["value"]=> int(10) ["compare"]=> string(1) "=" ["alias"]=> string(14) "mdvat_postmeta" ["cast"]=> string(4) "CHAR" } } ["has_or_relation":protected]=> bool(false) } ["date_query"]=> bool(false) ["request"]=> string(502) "SELECT SQL_CALC_FOUND_ROWS mdvat_posts.ID FROM mdvat_posts INNER JOIN mdvat_postmeta ON ( mdvat_posts.ID = mdvat_postmeta.post_id ) WHERE 1=1 AND mdvat_posts.ID IN (12501,12502,12503,12506,12510,12513,12528,12529,12536,12557,12558,12561,12565,12575,12576,12578,12587,12601,12602,12603,12604) AND ( ( mdvat_postmeta.meta_key = '_customer_user' AND mdvat_postmeta.meta_value = '10' ) ) AND mdvat_posts.post_type = 'shop_order' GROUP BY mdvat_posts.ID ORDER BY mdvat_posts.post_date DESC LIMIT 0, 20" ["posts"]=> array(0) { } ["post_count"]=> int(0) ["current_post"]=> int(-1) ["in_the_loop"]=> bool(false) ["comment_count"]=> int(0) ["current_comment"]=> int(-1) ["found_posts"]=> int(0) ["max_num_pages"]=> int(0) ["max_num_comment_pages"]=> int(0) ["is_single"]=> bool(false) ["is_preview"]=> bool(false) ["is_page"]=> bool(false) ["is_archive"]=> bool(true) ["is_date"]=> bool(false) ["is_year"]=> bool(false) ["is_month"]=> bool(false) ["is_day"]=> bool(false) ["is_time"]=> bool(false) ["is_author"]=> bool(false) ["is_category"]=> bool(false) ["is_tag"]=> bool(false) ["is_tax"]=> bool(true) ["is_search"]=> bool(false) ["is_feed"]=> bool(false) ["is_comment_feed"]=> bool(false) ["is_trackback"]=> bool(false) ["is_home"]=> bool(false) ["is_404"]=> bool(false) ["is_embed"]=> bool(false) ["is_paged"]=> bool(false) ["is_admin"]=> bool(true) ["is_attachment"]=> bool(false) ["is_singular"]=> bool(false) ["is_robots"]=> bool(false) ["is_posts_page"]=> bool(false) ["is_post_type_archive"]=> bool(false) ["query_vars_hash":"WP_Query":private]=> string(32) "c6d527f823896b8b9484557b2d167ae6" ["query_vars_changed":"WP_Query":private]=> bool(true) ["thumbnails_cached"]=> bool(false) ["stopwords":"WP_Query":private]=> NULL ["compat_fields":"WP_Query":private]=> array(2) { [0]=> string(15) "query_vars_hash" [1]=> string(18) "query_vars_changed" } ["compat_methods":"WP_Query":private]=> array(2) { [0]=> string(16) "init_query_flags" [1]=> string(15) "parse_tax_query" } }

any help will be appreciated.

1

The WP_Query::have_posts takes into consideration just the current_post and post_count properties of the object ( see the Code Reference for more details ). And those are, in deed, empty in your var_dump:

["posts"]=> array(0) { } ["post_count"]=> int(0) ["current_post"]=> int(-1)

So perhaps the query args are not correct and you are attempting to query something what can't be found in the database.

You can try to run the MySQL query directly in the database to see whether it's matching anything:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts 
INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE 1=1
    AND wp_posts.ID IN   
        (12501,12502,12503,12506,12510,12513,12528,12529,12536,12557,12558,
         12561,12565,12575,12576,12578,12587,12601,12602,12603,12604)
    AND ( ( wp_postmeta.meta_key = '_customer_user' AND wp_postmeta.meta_value = '10' ) )
    AND wp_posts.post_type = 'shop_order'
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
LIMIT 0, 20

I would specifically checked whether the post IDs are correct and whether they have the necessary post_meta with matching meta_value.

1
  • 1
    damn!!!!!! Dude! I don't know How Can I say thanks when I have No enough reputation in this community to vote you up! The query was wrong. You saved me. @david.binda – SirSaleh Sep 7 '16 at 19:29

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.