1

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.

4
  • 1
    LIKE comparisons are quite expensive to run. Also, serialized data is really not meant for searching and ordering operations. The issue here is with LIKE as well is, if you search for dot, mydot, dot, dotcom are also returned Feb 11, 2016 at 4:32
  • Thanks for the note on LIKE @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 (about dot) - could you elaborate?
    – rpbtz
    Feb 11, 2016 at 11:10
  • 1
    No problem. What the LIKE comparator do is, it does not search for exact mathes like a comparator like maybe IN would do. It searches, for anything that is likely to match the term. If you where to search for a custom field value of say dot, or lets say as in your example Data, the like comparator would search and return anything with those 4 letters in sequence of data, so it would return Data, data-mining, database` and crappy-data if you have those 4 words in database. So this make the LIKE very unreliable, which is why you should avoid serialized data Feb 11, 2016 at 11:19
  • Ah, that makes sense. Thank you. The data 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.
    – rpbtz
    Feb 11, 2016 at 11:34

1 Answer 1

1

I found an older post showing what I was missing from my code, so it seems like I didn't do a good enough search to begin with.

For anyone wondering I was missing 'compare' => 'LIKE' from the query, so the code would look like this:

$args_post = array('post_type' => 'release', 'orderby' => '_custom', 'order' => 'ASC', 'posts_per_page' => -1, 'meta_query' => array(
    array(
        'key' => 'classification',
        'value' => 'Data',
        'compare' => 'LIKE',
    ),
  ), 
);

Everything's working like a charm now!

UPDATE

While this solution works, please note @PieterGoosen's comments to the question:

LIKE comparisons are quite expensive to run. Also, serialized data is really not meant for searching and ordering operations. The issue here is with LIKE as well is, if you search for dot, mydot, dot, dotcom are also returned

What the LIKE comparator do is, it does not search for exact mathes like a comparator like maybe IN would do. It searches, for anything that is likely to match the term. If you where to search for a custom field value of say dot, or lets say as in your example Data, the like comparator would search and return anything with those 4 letters in sequence of data, so it would return Data, data-mining, database and crappy-data if you have those 4 words in database. So this make the LIKE very unreliable, which is why you should avoid serialized data

2
  • 1
    Not many reads comments, so feel free to include my comments into your answer to make my comments useful ;-) Feb 13, 2016 at 18:44
  • They're added now. You're right - should've just posted them to begin with. Easier for everyone :-)
    – rpbtz
    Feb 13, 2016 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.