1

This is the pattern that i'm trying to achieve with wp_query. It's going to be for the same post type. Is it possible to do so with wp_query?

Thank you!

        <div>

          <div>1st post from featured</div>

          <div>1st post not from featured</div>

          <div>2nd post from featured</div>

          <div>3rd post from featured</div>

          <div>2nd post not from featured</div>

          <div>3rd post not from featured</div>

          ..and then the same pattern start again with the rest of the posts

          <div>4th post from featured</div>

          <div>4th post not from featured</div>

          <div>5th post from featured</div>

          <div>6th post from featured</div>

          <div>5th post not from featured</div>

          <div>6th post not from featured</div>

          ..until there's no more post
        </div>

1 Answer 1

1

It becomes slightly more complicated. We need two WP_Query objects, some calculations and a for-loop.

For PHP to know about the right format, we can use a simple format array. In following example, I will be using categories to determine the two WP_Query builds.

First, we build the base variables:

<?php 

    // Build format
    // 0 means not featured - 1 means featured.
    $format = array(1, 0, 1, 1, 0, 0);

    // Retrieve normal posts EXCLUDING category with ID 11 (Featured)
    $normal_posts = new WP_Query(array(
        'post_type'      => 'post',
        'posts_per_page' => 999,
        'cat'            => '-11'
    ));

    // Retrieve featured posts ONLY INCLUDING category with ID 11 (Featured)
    $featured_posts = new WP_Query(array(
        'post_type'      => 'post',
        'posts_per_page' => 999,
        'cat'            => 11
    ));

    // Calculate total amount of posts
    // from our two WP_Query results
    $posts_total = $normal_posts->post_count + $featured_posts;

    // Setup current post index
    $post_index = 0;

Next, we start 'the loop' or iteration:

    // Iterate every post from two queries
    for($i = 0; $i < $posts_total; $i++){

In which we calculate current format index:

        // Calculate which type of post to display based on post index
        // We use modulo to get the right $format index
        $post_format_index = $post_index % count($format);
        $current_format    = $format[$post_format_index];

Finally, display the right type of post:

        // Display to frontend based on format
        switch ($current_format) {
            case 1:
                // Featured
                $featured_posts->the_post();

                the_title();

                // Your content

                print '<br>';
                break;

            default:
                // Not Featured
                $normal_posts->the_post();

                the_title();

                // Your content

                print '<br>';
                break;
        }

        $post_index++;
    }

?>

That's it. You could take this a step further by adding another WP_Query and format.

$format = array(1, 0, 1, 1, 2, 2);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.