Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I got the custom_post_type_data in which I store some variables when I make a new post. Variables likes Author, Price etc. I implemented a filtering by author, so when you click on James (for example), you will see all James`s products. All seemed nice until now. Today I discovered a little bug (At James products page, there are some other authors products listed). I do not know why exactly, but I guess I will need to be more specific, by adding a "%" after $author (James). The problem is that this will not work at all. Any idea why?

  'meta_query' => array(
    array(
        'key' => 'custom_post_type_data',
        'value' => $author,
        'compare' => 'LIKE',
    )
share|improve this question
add comment

1 Answer

... I will need to be more specific, by adding a "%" after $author (James). The problem is that this will not work at all. Any idea why?

The % is a wildcard with a MySQL LIKE query, which I suspect you know. With meta queries, WordPress will add one to the beginning and one to the end of the string you provide. You can see that in the source. or just by var_dumping the query. By adding a % of your own, I suspect is throwing things off. By the time WP_Query and wpdb is through with it, it ends up looking like \\%%. I just ran a query through to check that.

Unfortunately the LIKE meta_query is not very flexible, but maybe you could sort it out with the get_meta_sql filter.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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