Hello and thank you for this wonderful plugin!
I use a custom post type and I need to filter them by type, the type being a “Select” field called “tipo_lemma”. I have selected the option “Select multiple” values” because some posts have more than one type.
Now, I need to show all the posts that share a certain type, both those which have a single type and those which have multiple value.
For example, if I have:
post1 = typeA
post2 = typeA, typeB
If I filter by “typeA” I need to see both of them. If I filter by “typeB” just the second one.
If I use this code, it shows only those with a single type, while those with more than one type don’t appear at all:
if (isset($_GET["type"]) && $_GET["type"]!='') {
$add_args[meta_key] = 'tipo_lemma';
$add_args[meta_value] = $_GET["type"];
If I use this code, it’s the opposite: the page displays only the posts with more than one type, while those with only one type don’t appear (with some exceptions: strangely it shows also some posts with only one type when there is a link in another wysiwyg field – perhaps because I use a str_replace script, or because the full query is kind of complex):
if (isset($_GET["type"]) && $_GET["type"]!='') {
$add_args[meta_query] = array(
array(
'key' => 'tipo_lemma',
'value' => serialize(strval($_GET["type"])),
/*It's the same with: 'value' => '"'.$_GET["type"].'"',*/
'compare' => 'LIKE'
)
);
}
Any advice and suggestions will be greatly appreciated!
Hi @galadh
If the query is like this: &type=typeA,typeB
, then you need to add a query for each type listed. It should be something like this:
if (isset($_GET["type"]) && $_GET["type"]!='') {
// get the listed types in an array
$types = explode(',', $_GET[ "type" ]);
// initialize the meta query
$add_args['meta_query']['relation'] = 'OR';
// loop through the types and add it to the query
foreach( $types as $type ){
$add_args['meta_query'] []= array(
'key' => 'tipo_lemma',
'value' => '"'. $type .'"',
'compare' => 'LIKE'
);
}
}
I hope this helps π
It came out there was another problem (my fault!): there were almost 300 posts when I switched on the option “Select multiple values”, so I guess all the old posts still have a string in the field “tipo_lemma”, while the new ones have an array whether they have one or more type selected.
In fact, if I change the option “Return value” from “Label” to “Array” nothing happens, but when I open and save again the old posts, then it works. I just need to re-save all the posts then.
Thank you for the support! Your code works perfectly, so I’ll mark the question as solved π
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!
Accordions are a great way to group related information while allowing users to interactively show and hide content. In this video, Damon Cook goes in-depth on how to create an accessible accordion block using ACF PROβs Repeater field.https://t.co/RXT0g25akN
— Advanced Custom Fields (@wp_acf) March 2, 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.