WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How could you display all the post in a loop with a (custom) meta_value containing a word/phrase? for exemple get al the post where meta contains 'test'.

query_posts('post_type=SOME&category_name_2=SOME&meta_value= 'Cotains the word 'test' ??? '); 

so when the meta tags are 'test movie' or just 'test' you get them both.?

share|improve this question

No need for a custom query. Since 3.1, you can use a 'meta query';

query_posts( array(
    'meta_query' => array(
            'key' => 'meta_key_name',
            'value' => 'test',
            'compare' => 'LIKE'
) );

Check out the codex on custom field parameters.

share|improve this answer
You lack one more array level in meta_query and % are added automatically when using LIKE compare. – Rarst Jun 1 '11 at 21:49
@Rarst thanks, updated. – TheDeadMedic Jun 2 '11 at 10:16

by Using "Custom select query"


 $querystr = "
    SELECT wposts.* 
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id 
    AND wpostmeta.meta_key = 'tag' 
    AND wpostmeta.meta_value LIKE '%test%' 
    AND wposts.post_status = 'publish' 
    AND wposts.post_type = 'post'  
    ORDER BY wposts.post_date DESC

 $pageposts = $wpdb->get_results($querystr, OBJECT);

 <?php if ($pageposts): ?>
  <?php global $post; ?>
  <?php foreach ($pageposts as $post): ?>
    <?php setup_postdata($post); ?>
  <?php endforeach; ?>
 <?php endif; ?>
share|improve this answer

Your Answer


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.