There is a simple and effective way to split your bunch of posts inside your theme. You will need a simple get_posts() or WP_Query functions and that’s all!

First we need to search for our posts

<?php 

//For more details about this function see WordPress Codex

$args = array(
    'posts_per_page'   => 10,
    'offset'           => 0,
    'category'         => 'YOUR CATEGORY ID',
    'category_name'    => 'YOUR CATEGORY NAME',
    'orderby'          => 'date',
    'order'            => 'DESC',
    'include'          => '',
    'exclude'          => '',
    'meta_key'         => '',
    'meta_value'       => '',
    'post_type'        => 'post',
    'post_mime_type'   => '',
    'post_parent'      => '',
    'author'       => '',
    'author_name'      => '',
    'post_status'      => 'publish',
    'suppress_filters' => true 
);

$posts_array = get_posts( $args ); ?>

You fill the category or category_name with the category which from you want to display your posts.

The next step is to create the foreach function inside of it we can extract the data of the get_posts. To help ourselves we setup a counter $i to count the posts one by one. After that you need an if statement to define how many posts you need to display in column one. In my example i have 3. Next process is to gather all the ids of posts that have been displayed in an array. Now we can display for example the title of each post.

<?php $gather_post_ids = array(); ?>

<?php foreach ($posts_array as $post): $i++?>

    <?php setup_postdata( $post ); ?>

        <?php if ($i <= 3 ): ?>

            <?php array_push( $gather_post_ids, $post->ID ); ?>

            <?php the_title(); ?>

        <?php endif;?>

<?php endforeach; ?>

    <?php wp_reset_postdata(); ?>

Final step is to pass in another row or column the remaining posts. So we have to do again one simple WP_Query and to run foreach loop one more time in order to display the posts.

<?php

    $new_posts = new WP_Query( 
        array(  'post_type' => 'YOUR CUSTOM POST TYPE',
                'post__in' => $gather_post_ids,
                'posts_per_page'=> -1
    )); 

?>

And the whole final code should be looking like this:

<?php 

$args = array(
    'posts_per_page'   => 10,
    'offset'           => 0,
    'category'         => '',
    'category_name'    => 'category_name',
    'orderby'          => 'date',
    'order'            => 'DESC',
    'include'          => '',
    'exclude'          => '',
    'meta_key'         => '',
    'meta_value'       => '',
    'post_type'        => 'post',
    'post_mime_type'   => '',
    'post_parent'      => '',
    'author'       => '',
    'author_name'      => '',
    'post_status'      => 'publish',
    'suppress_filters' => true 
);

$posts_array = get_posts( $args ); ?>


<?php $gather_post_ids = array(); ?>

<?php foreach ($posts_array as $post): $i++?>

    <?php setup_postdata( $post ); ?>

        <?php if ($i <= 3 ): ?>

            <?php array_push( $gather_post_ids, $post->ID ); ?>

            <?php the_title(); ?>

        <?php endif;?>

<?php endforeach; ?>

    <?php wp_reset_postdata(); ?>


<?php

    $new_posts = new WP_Query( 
        array(  'post_type' => 'YOUR CUSTOM POST TYPE',
                'post__in' => $gather_post_ids,
                'posts_per_page'=> -1
    )); 

?>

<?php foreach ($new_posts as $post): ?>

    <?php setup_postdata( $post ); ?>

            <?php the_title(); ?>

<?php endforeach; ?>

    <?php wp_reset_postdata(); ?>

 

 

WordPress Support Services

Do you need a super fast and optimized website like ours?
We do things different at BakeMyWP!

Get a Free Quote

Leave a Reply

Your email address will not be published. Required fields are marked *