admin管理员组文章数量:1391960
I'm writing plugin for syncing prices of WooCommerce products with external source. So I want to get products with EAN (have meta data _wpm_gtin_code
). Whaterver I pass to get_posts($args)
I get empty data. I'm using WordPress 5.3.2.
I dived into WP source to track why. When I print generated SQL $this->request
at wp-includes/class-wp-query.php:2890
I see it contains "where condition" AND (0 = 1)
which obviously cannot succeed. This piece of code is inserted by Taxonomies called on wp-includes/class-wp-query.php:2101-2109
, explicitly by $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' ); $where .= $clauses['where'];
.
I didn't dig deeper into $this->tax_query->get_sql(...)
. What I know is that I don't pass any taxonomy arguments to get_posts($args)
, so I don't expect it to influence the query.
How shall I call get_posts($args)
to get all posts just of some type and status?
$meta_query = array(
'relation' => 'OR',
array(
'key' => '_wpm_gtin_code',
'compare' => 'EXISTS'
),
array(
'key' => '_product_code',
'compare' => 'EXISTS'
)
);
$args = array(
'meta_query' => $meta_query,
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
);
error_log(print_r($args, true)); // print args
$posts = get_posts( $args );
error_log(print_r($posts, true)); // print posts - empty!!!
I'm writing plugin for syncing prices of WooCommerce products with external source. So I want to get products with EAN (have meta data _wpm_gtin_code
). Whaterver I pass to get_posts($args)
I get empty data. I'm using WordPress 5.3.2.
I dived into WP source to track why. When I print generated SQL $this->request
at wp-includes/class-wp-query.php:2890
I see it contains "where condition" AND (0 = 1)
which obviously cannot succeed. This piece of code is inserted by Taxonomies called on wp-includes/class-wp-query.php:2101-2109
, explicitly by $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' ); $where .= $clauses['where'];
.
I didn't dig deeper into $this->tax_query->get_sql(...)
. What I know is that I don't pass any taxonomy arguments to get_posts($args)
, so I don't expect it to influence the query.
How shall I call get_posts($args)
to get all posts just of some type and status?
$meta_query = array(
'relation' => 'OR',
array(
'key' => '_wpm_gtin_code',
'compare' => 'EXISTS'
),
array(
'key' => '_product_code',
'compare' => 'EXISTS'
)
);
$args = array(
'meta_query' => $meta_query,
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
);
error_log(print_r($args, true)); // print args
$posts = get_posts( $args );
error_log(print_r($posts, true)); // print posts - empty!!!
Share
Improve this question
asked Mar 4, 2020 at 7:40
Martin EdlmanMartin Edlman
1112 bronze badges
1 Answer
Reset to default 1The whole problem was in WooCommerceCategoryExcluder
(old, obsolete and unmaintained) plugin which hooks to pre_get_posts
and adds taxonomy query to get_posts()
args.
Maybe this investigation helps someone else trying to solve similar problem.
本文标签: meta querygetposts( args ) return empty data
版权声明:本文标题:meta query - get_posts( $args ) return empty data 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744697884a2620388.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论