0

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!!!
1

The 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.

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.