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 :)