获取具有特定元键的所有帖子,但具有特定标题的帖子除外

时间:2014-11-18 作者:Gestalten

我需要返回所有帖子post_test_safe == 1 并排除带有title ="Special title"

某种组合元查询,但没有任何工作。

function getAllPostIdsTest(){
     $args = array(
        \'post_type\' => \'post_test\',
        \'posts_per_page\' => -1,
        \'meta_query\' => array(
            array(
                \'key\' => \'post_test_safe\',
                \'value\' => 1,
                \'compare\' => \'EQUALS\'
            ),
        ),
        \'fields\' => \'ids\',
        \'orderby\' => \'asc\'
    );
    $ids = get_posts( $args );
    send_json($ids);
}

1 个回复
SO网友:BODA82

目前无法对此进行测试,但请尝试以下方法。在所有帖子中搜索您的帖子名称(特殊标题),获取ID,然后利用post__not_in 在中排除这些IDget_posts.

function getAllPostIdsTest(){

     global $wpdb;
     $excluded_posts = $wpdb->get_results("SELECT id FROM " . $wpdb->posts . " WHERE `post_title` LIKE \'%"Special title"%\' && `post_type` = \'post_test\'");

     $args = array(
        \'post_type\' => \'post_test\',
        \'posts_per_page\' => -1,
        \'meta_query\' => array(
            array(
                \'key\' => \'post_test_safe\',
                \'value\' => 1,
                \'compare\' => \'=\'
            ),
        ),
        \'fields\' => \'ids\',
        \'orderby\' => \'asc\',
        \'post__not_in\' => $excluded_posts
    );

    $ids = get_posts( $args );
    send_json($ids);

}
编辑:post__not_in 需要一个简单的ID数组,ex//array(1,2,3,4)

结束

相关推荐