Forums

How to query multiple meta values under one meta key (5 posts)

  1. sirgeordie
    Member
    Posted 3 years ago #

    I'm currently trying to query multiple meta_values under one meta_key - rating.

    At present my query is the following:

    <?php $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(); $wp_query->query('cat=18,42&meta_key=rating&meta_value=8&9&10'.'&paged='.$paged); ?>

    This only shows items with a meta_value of 8 though, not 8, 9, and 10 as I want it to. I've tried separating the values with commas however that doesn't do anything either. Does anyone have any advice?

  2. sirgeordie
    Member
    Posted 3 years ago #

    After much looking around, I've realised that this is what I need:

    <?php

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

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

    ?>

    That's pretty much sorted. All I want to do now is show posts with a rating of 8, 9, and 10 in the same list, instead of just 8. Anyone have any ideas?

  3. sirgeordie
    Member
    Posted 3 years ago #

    Oh, and somehow figure out how to implement pagination into the query and only select posts from category 18 and 42.

    Anyone?

  4. na3s
    Member
    Posted 3 years ago #

    In response to your first post, it wasn't working because you were using '&' where you should ahve been using ','

    like so:

    <?php $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(); $wp_query->query('cat=18,42&meta_key=rating&meta_value=8,9,10'.'&paged='.$paged); ?>

  5. Otto
    Tech Ninja
    Posted 3 years ago #

    Doesn't matter, you can't do that in a query. WordPress' query system does not support multiple selection on the meta_values.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.