Search code examples
phpwordpresswordpress-themingcustom-post-type

Posts not ordering by post__in


I am trying to display 3 blog posts that have been selected by the user in the admin. The order should go gridItem1, gridItem2 then gridItem3. I have set out my query below but it shows the selected posts but in date order not in the order I have outlined.

I have searched other posts and added in the 'surpress_filters' => true and the wp_reset_query(); but neither seemed to have helped.

Here is my code:

    $gridItem1 = get_field('large_grid_item');
        $gridItem2 = get_field('second_grid_item');
        $gridItem3 = get_field('third_grid_item');        

        // Example argument that defines three posts per page. 
        
        
$args = array(
    'posts_per_page' => 3,
        'post_type' => array('post', 'page','programme'),
    'suppress_filters' => true,
    'post__in' => array($gridItem1, $gridItem2, $gridItem3),
    'order_by' => 'post__in',
);  


        
 
// Variable to call WP_Query. 
wp_reset_query();        
        
$the_query = new WP_Query( $args ); 

Does anyone know how to make them show in the order I have outlined? Thanks


Solution

  • You don't need to use suppress_filters. You don't need wp_reset_query either. The main problem is order_by which is incorrect. The correct form of using it is orderby.

    Use the following query instead:

    $gridItem1 = get_field('large_grid_item');
    $gridItem2 = get_field('second_grid_item');
    $gridItem3 = get_field('third_grid_item');
    
    $args = array(
        'post_type'        => array('post', 'page','programme'),
        'posts_per_page'   => 3,
        'post__in'         => array($gridItem1, $gridItem2, $gridItem3),
        'orderby'          => 'post__in',
    ); 
    
    $the_query = new WP_Query( $args );
    
    wp_reset_postdata();
    

    WP_QueryDocs

    Let me know if you were able to get it to work!