I use the following code to display posts from custom post type

query_posts( array( 'post_type' => 'success-stories', 'paged' => $paged, 'posts_per_page' => -1, 'meta_key' => 'post_id', 'orderby' => 'meta_value_num', 'order' => 'ASC' ) ); 

An arbitrary post_id field is set in order to be able to manually sort the posts (in the required order). But it is also necessary that the posts for which this parameter is not specified are displayed. Tell me, what needs to be changed in the request parameters?

    2 answers 2

    using query_posts not safe.

    Use get_posts , pre_get_posts or WP_Query .

    https://developer.wordpress.org/reference/functions/query_posts/
    https://codex.wordpress.org/Function_Reference/get_posts
    https://developer.wordpress.org/reference/hooks/pre_get_posts/
    https://codex.wordpress.org/Class_Reference/WP_Query

    'meta_key' => 'post_id'

    The names cannot use "post_id". This is a reserved term.

    The request should be done by OR , choosing both those posts where post_id not defined, or those where it is greater than zero:

     $args = array( 'post_type' => 'success-stories', 'paged' => $paged, 'posts_per_page' => -1, 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'post_id', 'compare' => 'NOT EXISTS', 'value' => '' ), array( 'key' => 'post_id', 'compare' => '>', 'value' => '0' ) ), 'orderby' => 'meta_value_num', 'order' => 'ASC' ); query_posts($args); 
    • In this case, displays all posts, except those where post_id is filled - Vlad
    • 'relation' => 'OR' did not miss? - KAGG Design
    • No, I tried to change the value even for another (AND) - Vlad
    • and what values ​​does post_id accept? Is more than zero everything, for example? - KAGG Design
    • Yes, any number is greater than 0 - Vlad