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.