meta_query compare LIKE doesn't show items when empty - wordpress

I have a loop that I am building a filter for, using a custom taxonomy.
It works, but only when there is a value entered. When the page loads it shows no entries, but when I filter by a value it shows the items with that tag. I thought that 'LIKE' would allow it show all entries while the value is empty.
I have tried this by removing the $ingredients and replacing it with an empty ''.
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'ingredients',
'value' => $ingredients,
'compare' => 'LIKE'
)
)
);
$query = new WP_Query($args);
while($query->have_posts()) : $query->the_post();?>
<?php the_title(); ?><br />
<?php endwhile; wp_reset_query(); ?>

Check by passing false to the argument,
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'ingredients',
'value' => $ingredients,
'compare' => 'LIKE'
),
array(
'key' => 'ingredients',
'value' => false,
'type' => 'BOOLEAN'
),
)
);
Also add compare => like to the false condition and cross check.

Related

Loop products excluding on sale shows only 4 items

I'm retrieving the latest products excluding on sale products based on a code example that i found (the original retrieved on sale products only). This is what i did:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 8,
'orderby' =>'id',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array( // Simple products type
'key' => '_sale_price',
'value' => 0,
'compare' => '=',
'type' => 'numeric'
),
array( // Variable products type
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '=',
'type' => 'numeric'
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
Actually it's retrieving only 4 products (i need 8) and these are not the latests, but it's excluding on sale products correctly.
Any idea? thank you.
(edited) This will handle simple products. I'm tried to find how to handle variations but couldn't find any meta key that is set when you create a sale price. Anyway, the "compare" shouldn't be "OR" but should be "AND", since you wish to find the products that does not have any of these meta values.
$args = array(
'post_type' => 'product',
'posts_per_page' => 8,
'orderby' =>'id',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => '_sale_price',
'compare' => 'NOT EXISTS',
),
)
);

Wordpress two loop exclude post

I am using two loop query:
<?php
// show all coupons marked Top Coupon
query_posts(array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
'meta_key' => 'clpr_topcoupon',
'meta_value'=> 1,
APP_TAX_STORE => $term->slug,
'ignore_sticky_posts' => 1,
'posts_per_page' => 1
));
?>
<?php get_template_part( 'loop3', 'coupon' ); ?>
<?php
query_posts( array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
APP_TAX_STORE => $term->slug,
'ignore_sticky_posts' => 1,
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'clpr_excoupon',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'clpr_excoupon',
'compare' => '!=',
'value' => '1'
),
),
) );
?>
<?php get_template_part( 'loop1', 'coupon' ); ?>
Now I don't want to show the first post from the first loop in the second loop. I tried get_the_ID(); however if this one is not having the 'meta_key' => 'clpr_topcoupon' one post is missing. How do I get the get_the_ID(); from first post but only if it has the 'meta_key' => 'clpr_topcoupon'?
Wordpress docs suggest that you should avoid using query_posts whenever possible stating:
Note: This function will completely override the main query and isn’t intended for use by plugins or themes. Its overly-simplistic approach to modifying the main query can be problematic and should be avoided wherever possible.
Instead we can use WP_Query . We'll use the first loop to store the post id and check it during the second loop. Maybe something like this:
<?php
//set parameters for First query
$args = array('post_type' => APP_POST_TYPE,
'post_status' => 'publish',
'meta_key' => 'clpr_topcoupon',
'meta_value'=> 1,
APP_TAX_STORE => $term->slug,
'ignore_sticky_posts' => 1,
'posts_per_page' => 1 );
$first_query = new WP_Query($args); // create query
$post_id = 0;
//initialize loop for custom query like this
if ($first_query->have_posts() ) {
while ($first_query->have_posts() ) {
$first_query->the_post();
$post_id = $post->ID; //store post ID outside of loop
get_template_part( 'loop3', 'coupon' );
}
}
wp_reset_postdata();
//setup second query
$args = array( //excludes post from query by ID See Bill erikson for complete list of WP_Query() arguments
'post__not_in' => array($post_id),
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
APP_TAX_STORE => $term->slug,
'ignore_sticky_posts' => 1,
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'clpr_excoupon',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'clpr_excoupon',
'compare' => '!=',
'value' => '1'
)
)
);
$second_query = new WP_Query($args);
if ($second_query->have_posts() ) {
while ($second_query->have_posts() {
$second_query->the_post();
get_template_part( 'loop1', 'coupon' );
}
}
wp_reset_postdata();
Hopefully, this code is able to assist you. As you can see, WP_Query accepts an argument 'post__not_in' that takes an array of page id's and excludes them from the query. We retrieved the id from the first query and referenced it in the argument of the second query. I also included wp_reset_postdata which is worth taking a look at if you're running multiple queries.
Good luck with your project!

query posts by ACF field not working

There are plenty of topics related to mine, but I still haven't found a solution. I'm trying to query posts by ACF field (radio button) and it seems that the meta_query gets completely ignored. It returns all the posts, instead of only those matching the criteria. I have tried using the field key instead of field name, other comparisons, etc. nothing seems to work. Hope you have an idea of what may be wrong! Here's my code:
<?php
$post_args = array(
'post_type' => 'products',
'posts_per_page' => - 1,
'status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'meta_key' => 'product_taste',
'meta_value' => array( 'cold' ),
'compare' => 'IN',
),
array(
'meta_key' => 'product_served',
'meta_value' => array( 'grated' ),
'compare' => 'IN'
)
),
);
$query = new WP_Query( $post_args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : ?>
<?php
$query->the_post();
?>
<h5>
<?php the_title(); ?>
</h5>
<?php endwhile ?>
<?php wp_reset_postdata();
}
?>
Use 'key' and 'value' in the 'meta_query' array
You don't need to use meta_key and meta_value in a meta_query... you only use those directly in the $args array. If you are adding a meta_query array, you just use key and value, e.g.
$post_args = array(
[...]
'meta_query' => array(
array(
'key' => 'product_taste',
'value' => 'cold',
'compare' => 'LIKE',
),
[...]
2. Querying serialised data with an array of values
There can also be issues using 'compare' => 'IN' with an array of values, when you are trying to query ACF data, because the ACF data can be serialised in the database (e.g. if data is in a repeater).
As you will only ever search for a single value, you can use LIKE instead of IN.
Putting these together
$post_args = array(
'post_type' => 'products',
'posts_per_page' => - 1,
'status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'product_taste',
'value' => 'cold',
'compare' => 'LIKE',
),
array(
'key' => 'product_served',
'value' => 'grated',
'compare' => 'LIKE'
)
),
);
$query = new WP_Query( $post_args );
If the data is serialized and you have values that could return multiple matches (e.g. LIKE 'cold' would match words like "cold", "colder","coldest"), then try adding a semicolon (;) at the end of the value, e.g.
[...]
array(
'key' => 'product_taste',
'value' => 'cold;', // add ; to the end of the value
'compare' => 'LIKE',
),
array(
'key' => 'product_served',
'value' => 'grated;', // add ; to the end of the value
'compare' => 'LIKE'
)
[...]
This will work when the values are serialised in the database, because each item will be separated by a semicolon.

Query for filter product by multi attribute and price in Wooecommerce?

I create a query like that:
<?php
$args = array(
'numberposts' => -1,
'post_type' => 'product',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'pa_color',
'value' => array('red'),
'compare' => 'IN',
),
array(
'key' => 'pa_for',
'value' => 'Men',
'compare' => '=',
),
),
);
$post = get_posts($args);
echo '<pre>';
print_r($post);
echo '</pre>';
When I try to run this code I get nothing. Please help me check this query and how do I get products by attribute and price?. I take meta_key in term_taxonomy table. Many thanks!

Checking meta_value in a loop with multiple choices

I can't get this to work correctly.
I want to loop through all child pages of the current page, find all the pages that have a 'status' of 'available' or 'coming Soon'.
The status field is an Advanced Custom Field select dropdown that has 6 options but I only want ones with available or coming soon.
Here is my current code. Its currently returning all child pages and ignoring the array check.
<?php $children = get_pages( array(
'child_of' => $post->ID,
'post_type' => 'property',
array(
'key' => 'status',
'value' => array('Available','Coming Soon'),
'compare' => 'IN'
)
));?>
if I do -
<?php $children = get_pages( array(
'child_of' => $post->ID,
'post_type' => 'property',
'meta_key' => 'status',
'meta_value' => 'Available',
));?>
Then it rightly returns just the 'available' posts but I want available and coming soon...
You are missing the meta_query and you should use get_posts
<?php
$children = get_posts(
array(
'child_of' => $post->ID,
'post_type' => 'property',
'meta_query' => array(
array(
'key' => 'status',
'value' => array('Available','Coming Soon'),
'compare' => 'IN'
)
)
)
);
?>

Resources