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.

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

2 Answers 2

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

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

Check out the codex on custom field parameters.

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

by Using "Custom select query"

<?php

 $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

 
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.