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.
Related
I want a query that returns CPT posts that contains the user's input (which is $text).
Inserting meta_key and meta_value works well but putting it in a meta_query array doesn't return anything. I want to be able to search in multiple custom fields. It's a theme I made from scratch so there is no plugins and the functions.php file is pretty small so I don't think it could be a conflict.
Code for meta_key and meta_value in the query declaration (working):
$searchquery = new WP_Query(
array( 'post_type' => 'offre',
'meta_key' => 'offre_employeur',
'meta_value' => $text,
'meta_compare'=> 'LIKE' )
);
Code for meta_value array (not working):
$meta_query_args = array(
'relation' => 'OR',
array(
'key' => 'offre_employeur',
'value' => $text,
'compare' => 'LIKE'
), array(
'key' => 'offre_titre',
'value' => $text,
'compare' => 'LIKE'
)
);
$searchquery = new WP_Query(array('post_type' => 'offre'));
$searchquery->set('meta_query', $meta_query_args);
Code for the second way I tried but still no results (not working)
$args = array(
'post_type' => 'offre',
'posts_per_page' => -1,
's' => $text,
'meta_query' => array(
array(
'key' => 'offre_employeur',
'value' => $text,
'compare' => 'LIKE'
),
array(
'key' => 'offre_titre',
'value' => $text,
'compare' => 'LIKE'
)
)
);
$searchquery = new WP_Query($args);
Thank you in advance for your time.
Per the docs here: https://codex.wordpress.org/Class_Reference/WP_Query
I would assert you want to scroll down to the section titled "Multiple Custom Field Handling:", which has this example, which most closely matches your situation:
$args = array(
'post_type' => 'product',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE',
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
);
$query = new WP_Query( $args );
Which, taken what you've provided in your question, I would modify as follows to get the results you are after:
$args = array(
'post_type' => 'offre',
'posts_per_page' => -1,
// remove the "search" query, which restricts the results to those with titles / content that match the $text content
// 's' => $text,
'meta_query' => array(
// add the "relation" argument, default is AND, you need OR
'relation' => 'OR',
array(
'key' => 'offre_employeur',
'value' => $text,
'compare' => 'LIKE'
),
array(
'key' => 'offre_titre',
'value' => $text,
'compare' => 'LIKE'
)
)
);
$searchquery = new WP_Query($args);
I would like to retrieve all those posts whose author (post table field) is a given one OR those which has a given meta value (postmeda table field).
If "author" was a meta value, I know I could use a meta_query to achieve it. The thing here is that it is not... so I think I cannot use the "author" field within a meta_query and use the "relation" key.
I'm looking for something like:
$args = array(
'post_type' => array('post'),
'orderby' => 'ASC',
'order' => 'date',
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'field' => 'author',
'value' => $author_id,
'compare' => '==',
),
array(
'key' => '_meta_field_name',
'compare' => 'NOT EXISTS',
),
),
array(
'relation' => 'AND',
array(
'key' => '_meta_field_name',
'compare' => 'EXISTS',
),
array(
'key' => '_meta_field_name',
'value' => $meta_field_value,
'compare' => '==',
),
),
),
);
$data = new WP_Query( $args );
Any suggestion on how to achieve that using WP_Query?
Thanks!
You might like to try an approach like this one instead. The idea is to query the two conditions you want to search for, then merge the two queries into one finished product.
//Get posts with the author you're looking for
$args1 = array(
'author_name' => 'testuser', //or 'author' => $author_id or something else
);
$data1 = get_posts( $args1 );
//Get posts with the meta data you're looking for
$args2 = array(
'meta_query' => array(
array(
'key' => 'meta_field_name',
'compare' => 'EXISTS',
),
array(
'key' => 'meta_field_name',
'value' => $meta_field_value,
'compare' => '==',
),
),
);
$data2 = get_posts( $args2 );
//Merge both arrays
$allData = array_merge( $data1, $data2 );
//Get just the IDs of all the posts found, while also dropping any duplicates
$postIDs = array_unique( wp_list_pluck( $allData, 'ID' ) );
//Do a new query with these IDs to get a properly sorted array of post objects
$args3 = array(
'post__in' => $postIDs,
'order' => 'ASC',
'orderby' => 'date',
);
$finalAnswer = get_posts( $args3 ); //This is your array of post objects. Ta-Da!
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.
I'm using this query do get some posts.
$args = array(
'post_type' => 'spaces',
'post_per_page' => '500',
'orderby' => 'rand',
'meta_key' => 'space-city',
'meta_value' => $search,
);
$query = new WP_query($args);
Now I need to order the results by total of comments on each post. I've a custom field with this number called "space-comments", but I've no idea how to sort this posts with this second meta_key.
I made some tests, but I only was able to get post when "space-comments" has a value. When there is no value, the post don't show up.
Any ideia how can I start?
The WP_Query can accept a meta_query argument that is populated as an array of sub-arguments. That array can have sub arrays that are each their own meta query, so you can create some nice compound searches across meta data.
See the example from https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters below.
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE',
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
);
$query = new WP_Query( $args );
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!