Support » Plugins » WordPress Custom Query With Multiple Meta Key Value

  • I am new with WordPress development and I have found this query and I want pass multiple key and value in this query.

    My Query is:

    $querydetails = "
       SELECT wposts.*
       FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
       WHERE wposts.ID = wpostmeta.post_id
    		AND wpostmeta.meta_key = 'type' AND wpostmeta.meta_value = 'Collection1'
       AND wposts.post_status = 'publish'
       AND wposts.post_type = 'book'
       ORDER BY wposts.post_date DESC
    ";

    I want add more statement with OR operator.

    My Original Code is:

    <?php
    $querydetails = "
       SELECT wposts.*
       FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
       WHERE wposts.ID = wpostmeta.post_id
    		AND wpostmeta.meta_key = 'type' AND wpostmeta.meta_value = 'Collection1'
       AND wposts.post_status = 'publish'
       AND wposts.post_type = 'book'
       ORDER BY wposts.post_date DESC
    ";
    
    $pageposts = $wpdb->get_results($querydetails, OBJECT);
    ?>
    
    <?php if ($pageposts):
    foreach ($pageposts as $post):
       setup_postdata($post); ?>
    
       <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
    
    <?php endforeach;
    endif; ?>

    Any Idea ??

    I will appreciate if you guide me 🙂

  • The topic ‘WordPress Custom Query With Multiple Meta Key Value’ is closed to new replies.