Support » Fixing WordPress » help with meta_key and multiple meta_value

  • hello,
    i use this to show the movie poste with the value Action :
    <?php query_posts('meta_key=movie&meta_value=Action'); ?>

    And i like to show mutli values : meta_value= Action and Science Fiction

    how can i do it?


Viewing 11 replies - 1 through 11 (of 11 total)
  • Since query_posts will only allow you to specify one meta_value you will have to either use wpdb to get all posts with both the Action and Science Fiction custom fields or use that existing query (all posts with custom field movie=Action, then when looping through the posts, ignore the posts that also have the Science Fiction custom field.

    Hard to understand for a newbe of php and wp

    ok, let’s say i have this type of movies : Drama, Action, Science Fiction, Animation, Comedy, Romantic Comedy

    so as i understand i need to delet all the value i dont need like this :

    i need => Action and Science Fiction
    i delet => Drama, Animation, Comedy and Romantic Comedy
    	DELETE FROM $wpdb->postmeta WHERE post_id = 'all'
    	AND meta_key = 'Drama,Animation,Comedy,Romantic Comedy'");

    any idea pls?

    DELETE would not be right.

    With the wpdb class use get_results or get_col.

    If you continue this thread please confirm if you are trying to
    return posts that are in “Action OR Science Fiction”


    posts that are in “Action AND Science Fiction”

    i would like to show only in page template, posts that are in “Action AND Science Fiction“.

    Help please.

    //get posts with two custom fields and two specific values
    $query="SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta, $wpdb->postmeta wpostmeta2
    WHERE wposts.ID = wpostmeta.post_id
    AND wposts.ID = wpostmeta2.post_id
    AND wpostmeta.meta_key = 'movie'
    AND wpostmeta.meta_value = 'Action'
    AND wpostmeta2.meta_key = 'movie'
    AND wpostmeta2.meta_value = 'Science Fiction'
    AND wposts.post_type = 'post'
    AND wposts.post_status = 'publish'
    ORDER BY wpostmeta.meta_value ASC";
    $results = $wpdb->get_results($query);
    if ($results) {
     echo ' <p>Results:</p>' ;
      foreach ($results as $post) {
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>

    when i test your code it show only one result. (i have more then 5 posts with the meta actions – Science Fiction.)

    Works for me–returns the two posts that have Custom Field movie and values of Action and Science Fiction.



    Here is my query that Works:

    Inner Join hbe_postmeta AS meta1 ON hbe_posts.ID = meta1.post_id
    Inner Join hbe_postmeta AS meta2 ON hbe_posts.ID = meta2.post_id
    meta1.meta_key =  'job-city' AND
    meta1.meta_value =  'New York' AND
    meta2.meta_key =  'job-department' AND
    meta2.meta_value =  'Business Development'

    Hopefully that helps 🙂



    Thanks nukecpower for ur reply,
    i dont know how i use ur code, any exemple for a newbe of PHP 🙂

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘help with meta_key and multiple meta_value’ is closed to new replies.