[resolved] Query posts by custom fields value (5 posts)

  1. eian00
    Posted 5 years ago #


    can I somehow display posts that satisfy the custom field values that I need:

    for ex, display all posts that have the value A from the field X, and value B from the field Y

    and I need to display them only if they have both the values i need.

    I was using the code below, but it shows posts that have values A or B, but I need to display them just if they have exactly the two i specify in the query loop.

    <?php query_posts('meta_key=color&meta_value=black
    &meta_key=shape&meta_value=circle&orderby=title&order=asc');  ?>
      <?php while (have_posts()) : the_post(); ?>
     <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>&nbsp;,&nbsp;
       global $wp_query;
       $postid = $wp_query->post->ID;
       echo get_post_meta($postid, 'color', true);
      <?php endwhile;?>

    How to filter that?

    thank you

  2. Mark / t31os
    Posted 5 years ago #

    The WP_Query class (what query_posts also uses) does not support querying for multiple meta keys.

    For multiple meta keys you've have to write your own query, see the wpdb class.

    Or you could do it this way, using a combination of the two (wpdb + query_posts/wp_query).

    // Get the IDs for posts with both meta keys and values
    $posts_with_meta = $wpdb->get_col("
    	SELECT ID from $wpdb->posts p
    		JOIN $wpdb->postmeta meta1 ON meta1.post_id = p.ID
    		JOIN $wpdb->postmeta meta2 ON meta2.post_id = p.ID
    	WHERE p.post_status = 'publish'
    	AND p.post_type = 'post'
    	AND meta1.meta_key = 'apples'
    	AND meta2.meta_key = 'oranges'
    	AND meta1.meta_value = 'juicy'
    	AND meta2.meta_value = 'sweet'
    // Create query object
    $my_query = new WP_Query();
    // Setup query paramters
    $my_query->query( array( 'post__in' => $posts_with_meta ) );
    // The above two lines could be replaced with the below instead
    // query_posts(  array( 'post__in' => $posts_with_meta )  );

    You could still plonk in your own parameters that way, ie. category, tag etc, as you usually would, we just use a custom query to go fetch the post IDs for posts with the relevant meta data first, then add that array of IDs into the post__in parameter to narrow down the results to only those posts.

  3. eian00
    Posted 5 years ago #

    ok nice, i found something out, the code i found is working just for one field, how can i modify the code below to join two fields and two values?

    global $wpdb;
    global $post;
     $querystr = "
        SELECT wposts.*
        FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
        WHERE wposts.ID = wpostmeta.post_id
        AND wpostmeta.meta_key = 'name'
        AND wpostmeta.meta_value = 'john'
        AND wpostmeta.meta_key = 'age'
        AND wpostmeta.meta_value = '26'
        AND wposts.post_type = 'post'
        ORDER BY wpostmeta.meta_value DESC
    $pageposts = $wpdb->get_results($querystr, OBJECT);

    any idea?

  4. arpitap
    Posted 5 years ago #

    Hey! Mark,

    Thnaks for the solution, it worked for me.

    Thnaks a lot.


  5. mariostella
    Posted 4 years ago #

    @ Mark / t31os
    I tried your query but it does not output anything the way it is, whereas if I use the
    query_posts( array( 'post__in' => $posts_with_meta ) );
    I get an infinite repetition of a specific post (dunno why that one in particular as it does not even satisfy the query parameters).

    Any idea? I am on 2.9.2

Topic Closed

This topic has been closed to new replies.

About this Topic