Home › Forums › General Issues › How to search for meta array?
Hi, I’m trying to search a post which have a meta value that contains one of these values: 1408 or 1407. Within the postmeta table I have this:
a:3:{i:0;s:4:”1406″;i:1;s:4:”1407″;i:2;s:4:”1408″;}
So I’ve created a function which contains the following:
function add_related_posts_to_search( $query ) {
if( $query->is_search ) {
$search_term = $query->query_vars['s'];
$query->query_vars['s'] = '';
$post_ids = search_medici_in_servizio_title( $search_term );
if( ! empty( $post_ids ) ) {
$meta_query = array();
foreach($post_ids as $pi) {
$meta_query[] = array(
'key' => 'medici',
'value' => sprintf("'%s'", $pi),
'compare' => 'LIKE',
);
}
$query->set('meta_query', $meta_query);
}
}
}
add_action( 'pre_get_posts', 'add_related_posts_to_search' );
essentially I’m getting all the id of the posts that have the searched term (in this case is returned an array with 1407 and 1408), and then I’ve mounted a meta_query that should return all the posts that contains one of the values specified.
For some reason I doesn’t get any result, any idea?
The meta query relation argument defaults to ‘AND’, you need to change it to ‘OR’
// start your meta query array with relation argument
$meta_query = array('relation' => 'OR');
Hi, thanks for the answer. Your solution has fixed the problem but I notice a strange behaviour.
Essentially, if I remove this line:
$query->query_vars[‘s’] = ”;
I doesn’t get any result. I would like to return all the posts which contains a certain meta, and also match as title or content the searched term.
Is there something else broken in my code?
To get results the search query would have to be present in title or content AND the meta query would have to return posts.
(Title match OR content match) AND meta match
I’m not sure if I understood correctly, maybe could I have a little example please?
Thanks in advance
You are searching for a number in the meta query. That means that the number must appear in either the title or the content
$title = ‘this is a title’
$content = ‘this is the content’
$meta = ‘a:3:{i:0;s:4:”1406″;i:1;s:4:”1407″;i:2;s:4:”1408″;}’
then
($title LIKE ‘%1406%’ OR $content LIKE ‘%1406%’) AND $meta LIKE ‘%”1406″%’
Is false
The only way the above can be true is if something like either
$title = ‘this is 1406 a title’
$content = ‘this is the content’
$meta = ‘a:3:{i:0;s:4:”1406″;i:1;s:4:”1407″;i:2;s:4:”1408″;}’
OR
$title = ‘this is a title’
$content = ‘this is the 1406 content’
$meta = ‘a:3:{i:0;s:4:”1406″;i:1;s:4:”1407″;i:2;s:4:”1408″;}’
OR
$title = ‘this is a 1406 title’
$content = ‘this is the 1406 content’
$meta = ‘a:3:{i:0;s:4:”1406″;i:1;s:4:”1407″;i:2;s:4:”1408″;}’
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.