WordPress.org

Support

Support » How-To and Troubleshooting » help with meta_key and multiple meta_value

help with meta_key and multiple meta_value

  • taghaboy
    Member

    @taghaboy

    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?

    thanks

Viewing 11 replies - 1 through 11 (of 11 total)
  • MichaelH
    Member

    @michaelh

    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.

    taghaboy
    Member

    @taghaboy

    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
    <?php
    $wpdb->query("
    	DELETE FROM $wpdb->postmeta WHERE post_id = 'all'
    	AND meta_key = 'Drama,Animation,Comedy,Romantic Comedy'");
    ?>

    taghaboy
    Member

    @taghaboy

    any idea pls?
    thanks

    MichaelH
    Member

    @michaelh

    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”

    or

    posts that are in “Action AND Science Fiction”

    taghaboy
    Member

    @taghaboy

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

    taghaboy
    Member

    @taghaboy

    Help please.

    MichaelH
    Member

    @michaelh

    <?php
    //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) {
        setup_postdata($post);?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      }
    }
    ?>
    taghaboy
    Member

    @taghaboy

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

    MichaelH
    Member

    @michaelh

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

    nukecpower
    Member

    @nukecpower

    Here is my query that Works:

    SELECT
    hbe_posts.*
    FROM
    hbe_posts
    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
    WHERE
    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 🙂

    taghaboy
    Member

    @taghaboy

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

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