Checking meta_value in a loop with multiple choices - wordpress

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'
)
)
)
);
?>

Related

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.

Filter with Advanced Custom Field in WordPress

I am trying to set filters based on Advanced Custom Fields in my WordPress site. Basically my advanced custom field named as 'ispremium' has two values as 'yes' and 'no' and in dropdown filter I have set two options as 'Premium Only' And 'All Programs'.
I need to do when 'Premium Only' dropdown option is selected it will list all the post having value 'ispremium'='yes' and when all program is selected it will list both 'ispremium=yes' and 'ispremium='no'. I have following code but it is listing post with 'ispremium=yes' always. What's wrong in my code?
<select name="order" onchange="this.form.submit();">
<?php
$order_options = array(
'yes' => 'Premium Only',
'no' => 'All Programs',
);
$result = query_posts( array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'ispremium',
'value' => 'yes',
),
) ) );
$result = query_posts( array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'ispremium',
'value' => 'yes','no'
),
) ) );
foreach( $order_options as $result => $label ) {
echo "<option ".selected( $_GET['value'], $value )." value='$value'>$label</option>";
}
?>
</select>
First, try to use WP_Query instead of query_posts(). You also have some syntax errors like 'value' => 'yes','no'.
For premium only:
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'ispremium',
'value' => 'yes',
'compare' => '='
)
)
);
$the_query = new WP_Query( $args );
All Programs
$args = array(
'post_type' => 'post',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ispremium',
'value' => 'yes',
'compare' => '='
),
array(
'key' => 'ispremium',
'value' => 'no',
'compare' => '='
)
)
);
$the_query = new WP_Query( $args );
Check ACF documentation for further info. Also, I suggest you to do this with Ajax, since if you put this code within your select element, the available options are going to be the from the last query made, and won't be dynamic on field change.
But I don't totally get what you're trying to do though; you'll probably want to show the available programs in another drop down, or you could simply redirect to another archive page which lists the programs accordingly to the selected option.

meta_query compare LIKE doesn't show items when empty

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.

WordPress meta_query for Custom Post Type with orderby

I'm trying to sort out a WordPress query for a Custom Post Type, but I can't make it work.
The post type is events. An Advanced Custom Fields field called sticky (yes/no) is used for sorting (stickies on top), and another ACF field called glamrock (yes/no) is used to exclude certain posts.
The result is, glamrock gets excluded, but stickies are not sorted.
If I delete the meta_query, sorting works fine, but glamrock posts are included.
$bpb_args = array(
'numberposts' => -1,
'post_type' => 'events',
'posts_per_page' => 100,
'paged' => get_query_var( 'paged', true ),
'meta-key' => 'sticky',
'meta_query' => array(
array(
'key' => 'glamrock',
'value' => 'no',
'compare' => 'IN',
)
),
'orderby' => 'meta_value',
'order' => 'ASC',
);
$bpb_query = new WP_Query( $bpb_args );
if( $bpb_query->have_posts() ):
while( $bpb_query->have_posts() ) : $bpb_query->the_post();
//show post
endwhile;
endif;
Update:
Unfortunately, meta_value instead of meta_value_num didn't change anything. It still seems to be sorting by date/time.
The Advanced Custom Field type is Radio Buttons.
In addition to the arguments, I also included the loop.
You need to specify only meta_value since your meta-key is non numeric
'orderby' => 'meta_value'
#Mihai had it right: meta_value_num was the main issue. I changed it to meta_value.
Here's the query/loop that worked:
$args = array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => 'sticky',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'lizenz_erteilt',
'value' => 'no',
'compare' => 'LIKE',
),
)
);
$query = new WP_Query( $args );
// Loop
if( $query->have_posts() ) :
while( $query->have_posts() ) : $query->the_post();
//show post
endwhile;
endif;

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!

Resources