0

I need to return all posts with post_test_safe == 1 and exclude one with title ="Special title"

Some kind of combined meta query but haven't got anything to work.

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

Unable to test this at the moment, but try something along these lines. Search all posts for your Post Name (Special title), grabbing the ID's, then utilize post__not_in to exclude those IDs in get_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);

}

Edit: post__not_in is expecting a simple array of ID's, ex// array(1,2,3,4)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.