I'm using Advanced Custom Fields to add meta information to a custom post type and in ACF I've created two different 'Select'-dropdowns - one is called 'series' the other 'classification'.
I run a query to display all posts with the 'series' term 'Compilation' using this code:
<?php
$args_post = array('post_type' => 'release', 'orderby' => '_custom', 'order' => 'ASC', 'posts_per_page' => -1, 'meta_query' => array(
array(
'key' => 'series',
'value' => 'Compilation',
),
),
);
$loop = new WP_Query($args_post);
while ($loop->have_posts() ) : $loop->the_post();
?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
And it displays the correct results perfectly.
However, when try a query with the 'classification' term 'Data' using this code:
<?php
$args_post = array('post_type' => 'release', 'orderby' => '_custom', 'order' => 'ASC', 'posts_per_page' => -1, 'meta_query' => array(
array(
'key' => 'classification',
'value' => 'Data',
),
),
);
$loop = new WP_Query($args_post);
while ($loop->have_posts() ) : $loop->the_post();
?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
Then it doesn't return anything. I've tried it with other terms in each field - the 'series' terms work perfectly, while none of the 'classification' terms work. I've double checked that I didn't do something as silly as forget to apply the page template to the right pages or forget to associate the posts with the terms, but everything looks like it's supposed to. I've checked for dumb typos as well, but everything checks out.
I did the code for the posts using 'series' terms first and then just straight copy-pasted it and changed the meta key and value to match the 'classification' posts afterwards, so other than these two minor changes (+ page template name) the code is identical.
Am I missing something obvious here?
UPDATE
So I figured out the obvious thing I was missing. 'series' terms can only be a single value, while 'classification' allows for multiple meaning it's an array. However, I still cannot seem to figure out how to display the correct posts. I've changed the line to 'value' => array('Data'),
but that doesn't really seem to change anything. I'm not sure if that's at all the correct approach either.
LIKE
comparisons are quite expensive to run. Also, serialized data is really not meant for searching and ordering operations. The issue here is withLIKE
as well is, if you search fordot
,mydot
,dot
,dotcom
are also returnedLIKE
@PieterGoosen. It works for now, but I'll try looking for a better alternative. I'm quite new to the php part of WP, so I'm not sure what you mean with the second part of your comment (aboutdot
) - could you elaborate?LIKE
comparator do is, it does not search for exact mathes like a comparator like maybeIN
would do. It searches, for anything that is likely to match the term. If you where to search for a custom field value of saydot
, or lets say as in your exampleData
, the like comparator would search and return anything with those 4 letters in sequence ofdata
, so it would returnData
, data-mining,
database` andcrappy-data
if you have those 4 words in database. So this make theLIKE
very unreliable, which is why you should avoid serialized datadata
value was just an example - the actual values I'm searching for are longer and more specific so I don't think I'll run into this issue. But this is good to know so I don't make that mistake in the future.