I’m trying to use the meta_query in WooCommerce product page.
This is the code I’m using:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' =>8,
'meta_query' => array(
array(
'key' => 'autor',
'value' => '"'.get_the_ID().'"',
'compare' => 'LIKE',
)
),
);
$products = new WP_Query($args);
if ($products->have_posts()) :
$i=0;
while ($products->have_posts()) : $products->the_post();
$autor = get_field('autor');
if($i % 2 ==0) ?>
<h3><?php the_title();?></h3>
<?php if ($i % 2 != 0)
$i++;
endwhile;endif;?>
It doesn’t show any title, if I remove the meta_query it shows all products so the problem is that the relation meta_query code is not working. Any ideas how to use it on WooCommerce template?
You use
get_the_ID()
to get the author id in meta_query args.get_the_ID()
– will get the Post id, not the author id.To get all posts by authoк id your args should look like this:
I also see that you using
get_field()
-function. WordPress core does not have this function. You can use insteadget_the_author()
.Eventually your code will look like: