79510517

Date: 2025-03-15 02:02:00
Score: 0.5
Natty:
Report link

Thank you very much to both wouch and disinfor for the super useful comments - performing two queries and merging was exactly what I needed to do. Following the link to the WP Stack Exchange, here's the code that does what I need:

// Go get the pinned articles
$pinnedArgs = array(
    'post_type' => 'post',
    'posts_per_page' => 5,
    'meta_query' => array(
        array(
            'key' => 'pinned',
            'value' => '1'
        )
    )
);
            
$pinnedResults = new WP_Query($pinnedArgs);

// Grab all the IDs to add to the args, otherwise we may duplicates
$foundIds = wp_list_pluck( $pinnedResults->posts, 'ID' );

$otherArgs = array(
    'post_type' => 'post',
    'posts_per_page' => 5,
    'post__not_in' => $foundIds
);
            
$otherResults = new WP_Query($otherArgs);

// Merge the two together and return this list
$mergedQuery = new WP_Query();
$mergedQuery->posts = array_merge( $pinnedResults->posts, $otherResults->posts );
$mergedQuery->post_count = $pinnedResults->post_count + $otherResults->post_count;

return $mergedQuery;
Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Blacklisted phrase (0.5): I need
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Richard Ockerby