dqmfo84644 2015-08-25 15:52
浏览 51
已采纳

多选从wordpress sql

I tried to get posts from wordpress sql, I want get posts where post_type = 'post' and where post_type = 'page'

My code:

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

This code is correct

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

but only post_type = 'posts' i want both! post and page.

About Wordpress SQL

Thanks.

  • 写回答

1条回答 默认 最新

  • doujianzi8521 2015-08-25 15:59
    关注

    You can use OR:

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

    ...but I still don't understand why you don't do a standard WP_Query().

    $args = array(
        'post_type' => array('post', 'page'),
        'orderby' => 'rand',
        'posts_per_page' => -1
    );
    $query = new WP_Query( $args ); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程