Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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; ?>

I have also tried with this code but when I pass 4 records nothing happening, only page is loading and loading.

<?php   
$args = array(


    //Type & Status Parameters
    'post_type'   => 'book',
    'post_status' => 'publish',

    //Order & Orderby Parameters
    'order'               => 'DESC',
    'orderby'             => 'date',

    //Pagination Parameters
    'posts_per_page'         => -1,
            // Here you can add you second meta value

    'meta_query'     => array(
                   'relation' => 'OR',
        array(
            'key' => 'type',
            'value' => 'Sample 1',
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'type',
            'value' => 'Sample 2',
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'type',
            'value' => 'Sample 3',
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'type',
            'value' => 'Sample 4',
            'compare' => 'LIKE'
        )
    )
);

$query = new WP_Query( $args );
?>

<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

<h2><a href="<?php the_permalink(); ?>"><?php echo the_title(); ?></a></h2>


<?php endwhile; ?>
<!-- post navigation -->
<?php else: ?>
<!-- no posts found -->
<?php endif; ?>

<?php 
// Restore original Post Data
wp_reset_postdata(); ?>

Any Idea ??

I will appreciate if you guide me :)

share|improve this question

3 Answers 3

up vote 1 down vote accepted

Try this:

<?php
    $querydetails = "
        SELECT wposts.*             
          FROM $wpdb->posts wposts
        INNER
          JOIN ( SELECT post_id
                   FROM $wpdb->postmeta wpostmeta
                        WHERE ( 
                            ( wpostmeta.meta_key = 'type' AND wpostmeta.meta_value LIKE '%Collection1%' )
                        OR 
                            ( wpostmeta.meta_key = 'type'AND wpostmeta.meta_value LIKE '%Collection2%' )
                        OR 
                            ( wpostmeta.meta_key = 'type'AND wpostmeta.meta_value LIKE '%Collection3%' )
                        OR
                            ( wpostmeta.meta_key = 'type'AND wpostmeta.meta_value LIKE '%Collection4%' )
                        OR
                            ( wpostmeta.meta_key = 'type'AND wpostmeta.meta_value LIKE '%Collection5%' )
                        )
                        GROUP BY post_id
                ) AS t
            ON t.post_id = wposts.ID
            WHERE 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; ?>

I hope this will works for you :)

share|improve this answer
// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
        echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
        echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

Insert an array containing your args for $args and you're done.

share|improve this answer
<?php   
$args = array(


    //Type & Status Parameters
    'post_type'   => 'book',
    'post_status' => 'publish',

    //Order & Orderby Parameters
    'order'               => 'DESC',
    'orderby'             => 'date',

    //Pagination Parameters
    'posts_per_page'         => -1,
            // Here you can add you second meta value

    'meta_query'     => array(
                   // ['relation'] => 'OR', // use this for a different comparison
        array(
            'key' => 'type',
            'value' => 'Collection1',
            'compare' => '='
        ),
        array(
            'key' => 'type2',
            'value' => 'Collection2',
            'compare' => '='
        )

);

$query = new WP_Query( $args );
?>

<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>


<?php endwhile; ?>
<!-- post navigation -->
<?php else: ?>
<!-- no posts found -->
<?php endif; ?>

<?php 
// Restore original Post Data
wp_reset_postdata(); ?>

I would use WP_Query and a meta_query, see https://codex.wordpress.org/Class_Reference/WP_Query for more info.

share|improve this answer
    
Hi. I have tried with this query but when I pass 4 records nothing happening. Only page is loading loading and loading. Any Idea? –  Manan Mar 5 at 2:35
    
Well I did not test this script but looks okay. What I would do is check the error log to make sure there is not a syntax, or minor error somewhere. If no errors, I would log the query quickly like error_log(print_r($query,true),3,get_template_directory() . '/temp_log_filename.log'); –  jjlm Mar 5 at 12:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.