多选择从wordpress sql


Multi select from wordpress sql

我试图从wordpress sql中获取帖子,我想获得post_type = 'post'和post_type = 'page'的帖子

我代码:

   $wpdb->get_row( " SELECT * FROM $wpdb->posts WHERE post_type = 'post' and post_type = 'page' AND post_status = 'publish' ORDER BY RAND() " );

这个代码是正确的

$wpdb->get_row( " SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY RAND() " );

但只有post_type = 'posts'我想要两个!Post and page.

关于Wordpress SQL

谢谢。

您可以使用OR:

$wpdb->get_row( " SELECT * FROM $wpdb->posts WHERE (post_type = 'post' OR post_type = 'page') AND post_status = 'publish' ORDER BY RAND() " );

…但我还是不明白你为什么不做一个标准的WP_Query()

$args = array(
    'post_type' => array('post', 'page'),
    'orderby' => 'rand',
    'posts_per_page' => -1
);
$query = new WP_Query( $args );