• I am trying to achieve the following. Using the wordpress theme newspaper, a custom block is displaying the four most recent posts from a couple of categories, which are set in a tagdiv composer that came with the theme.

    These blocks offer sorting options, which are built in the theme as “cases” or wp query arguments. These cases can then be selected and applied to the blocks where you set number of posts, categories on which to show posts fom etc.

    I however need the latest post from one category (called win win win), which if set to that category, automatically checks the custom field “wedstrijd” to true, with the thought of using this “true/false” boolean, to show up as the first post of the four.

    So, if there is a contest, the first post is always that contest post, the other three are to be filled up with the most recent posts from an array of categories.

    Until the contest post is deleted, it should remain on the top position while other content is being added, which thus will have a later publishing date. That contest category will always have just one active published post.

    I have tried the following but sadly I only get to show the single post with the key wedstrijd set to true. Anyone out there who can help me with this ?

    Sticky posts are sadly not an option since the theme doesn’t support this.

    The categories are set in the tagdiv composer of the site, which are 4,5,9,433,43,47,36,18,17,427,790,31648, with 31648 being the contest category. I have already tried setting the categories in the query below but this doesn’t work or override the settings in the tagdiv composer.

           case 'winfirst':
            $wp_query_args['meta_query'] = array(
                     'relation' => 'OR',
                     'query_one' => array(
                     'key' => 'wedstrijd',
                     'compare' => '=',
                     'value' => '1'
                                                 ),
                     'query_two' => array(
                     'key' => 'wedstrijd',
                     'compare' => '=',
                     'value' => '', 
        ), 
    );    
    $wp_query_args['orderby'] = array( 'query_one' => 'ASC', 'query_two' => 'DESC');
    • This topic was modified 5 years, 6 months ago by Jan Dembowski.
    • This topic was modified 5 years, 6 months ago by bcworkz. Reason: code fixed
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Even if your theme does not support sticky posts, WP_Query still would. But there are many situations where stickiness would not be applied, so you question is still valid. You could query for posts in a particular category, but order the results by meta_value. Every post will need a meta value, whether it is true or false. Order the meta values in descending order so that 1 (true) will be listed before the 0 (false) posts.

    You can supply a secondary orderby argument to further order the false meta value posts by another criteria such as date.

    BTW, when you post code in these forums, please demarcate with backticks or use the code button. Otherwise the forums’s parser will corrupt you code. I fixed the code in you OP for you.

Viewing 1 replies (of 1 total)
  • The topic ‘Show post first in loop based upon meta key, in array of multiple categories’ is closed to new replies.