WordPress.org

Ready to get started?Download WordPress

Forums

help with meta_key and multiple meta_value (12 posts)

  1. taghaboy
    Member
    Posted 3 years ago #

    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

  2. MichaelH
    Member
    Posted 3 years ago #

    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.

  3. taghaboy
    Member
    Posted 3 years ago #

    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'");
    ?>
  4. taghaboy
    Member
    Posted 3 years ago #

    any idea pls?
    thanks

  5. MichaelH
    Member
    Posted 3 years ago #

    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"

  6. taghaboy
    Member
    Posted 3 years ago #

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

  7. taghaboy
    Member
    Posted 3 years ago #

    Help please.

  8. MichaelH
    Member
    Posted 3 years ago #

    <?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
      }
    }
    ?>
  9. taghaboy
    Member
    Posted 3 years ago #

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

  10. MichaelH
    Member
    Posted 3 years ago #

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

  11. nukecpower
    Member
    Posted 3 years ago #

    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 :)

  12. taghaboy
    Member
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic