• Hello, how can I using Custom Select Query to display all posts having the same custom fields, not only one, but two or more?

    the example under gets posts by only one custom field value, how can I modify it to filter two or more meta keys and values?

    <?php
    global $wpdb;
    global $post;
    $querystr = "
    SELECT wposts.*
    FROM $wpdb->posts wposts
    	LEFT JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
    	LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id)
    	LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE wpostmeta.meta_key = 'customDateField'
    	AND wpostmeta.meta_value >= CURDATE()
    	AND $wpdb->term_taxonomy.taxonomy = 'category'
    	AND $wpdb->term_taxonomy.term_id IN(1,2)
    ORDER BY wpostmeta.meta_value ASC
    LIMIT 4
    ";
    ?>

    Thank you

Viewing 1 replies (of 1 total)
  • I think this is what you want:

    <?php
    global $wpdb;
    global $post;
    $querystr = "
    SELECT wposts.*
    FROM $wpdb->posts wposts
    	LEFT JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
    	LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id)
    	LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE
    ((wpostmeta.meta_key = 'customDateField' AND wpostmeta.meta_value >= CURDATE())
      OR (wpostmeta.meta_key = 'anotherkey' AND wpostmeta.meta_value = 'anothervalue')
      OR (wpostmeta.meta_key = 'yetanotherkey' AND wpostmeta.meta_value = 'yetanothervalue'))
    	AND $wpdb->term_taxonomy.taxonomy = 'category'
    	AND $wpdb->term_taxonomy.term_id IN(1,2)
    ORDER BY wpostmeta.meta_value ASC
    LIMIT 4
    ";
    ?>

    But, your ORDER BY clause is probably not what you want.

Viewing 1 replies (of 1 total)
  • The topic ‘Using Custom Select Query’ is closed to new replies.