我是WordPress开发的新手,我找到了这个查询,我想在这个查询中传递多个键和值。
我的问题是:
$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
";
我想添加更多语句
OR
操作人员
我的原始代码是:
<?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; ?>
我也尝试过这段代码,但当我传递4条记录时,什么都没有发生,只有页面正在加载和加载。
<?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(); ?>
有什么想法吗??
如果你能指引我,我将不胜感激:)