0
votes

I am trying to make it so that my custom post_type's will output in my pagination loop on the main page of my Wordpress site X amount of posts. However, it's not working.

Here is my code;

<?php if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); } else if ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); } else { $paged = 1; }
    $my_query = new WP_Query ( array (
        'post_type' => array ( 'reviews', 'blogtours', 'interviews', 'post' ),
        'posts_per_page'      => array( '1', '5', '0', '0' ),
        'paged'               => $paged, ) );
    while ( $my_query->have_posts() ) : $my_query->the_post(); ?>

I set 'interviews' and 'post' to 0 right now because they don't have any yet. I even tried removing them, thinking you couldn't set a post_per_page to 0 and that didn't work either (removing them from both posts_per_page and post_type).

The only thing that has worked so far is if I change;

'posts_per_page'      => array( '1', '5', '0', '0' ),

to

'posts_per_page'      => 3,

As an example. Then, it DOES show 3 posts, but in general from ALL post_types which is what I don't want. I want to be able to have it select from individual post_type. Because the "blogtours" post_type is going to have far more than any other and if I don't limit it to a smaller number, the other post_types will never been seen (unless you go to the front page, but I do a lot of posts per day so even that isn't feasible).

Any help would be most appreciated!

1

1 Answers

0
votes

You can't have an array for posts_per_page from what I have researched. It has to be one number.arw you trying to dump all of these posts out into the same feed or different pages? If different pages, you should separate out the queries. Posts per page is going to limit all of the posts to only show 3 total from the query. It's basically only going to grab 3 no matter what. If you separate out each query on the different posts, you can set the posts per page individually like the way you want. Then I would have a separate array that that you push all the different posts into, that then is fed onto your homepage. Does that make sense? Query on each post type so you can limit the amount of posts you get from the database, then push them into a master array of all of your posts.

Below are your four separate queries, but make sure I have the correct posts_per_page you want.

    $post_reviews = get_posts(
        'post_type'      => 'reviews',
        'post_status'    => 'publish',
        'posts_per_page' => 5
    );
    $post_blogtours = get_posts(
        'post_type'      => 'blogtours',
        'post_status'    => 'publish',
        'posts_per_page' => 3
    );
    $post_interviews = get_posts(
        'post_type'      => 'interviews',
        'post_status'    => 'publish',
        'posts_per_page' => 3
    );
    $post_post = get_posts(
        'post_type'      => 'post',
        'post_status'    => 'publish',
        'posts_per_page' => 5
    );

    // Below is where you merge all four arrays
    $all_posts = array_merge($post_reviews, $post_blogtours, $post_interviews, $post_post);

   // Sort through the posts, this might need modification based on your data in the array
    function sortFunction( $a, $b ) {
        return strtotime($a["date"]) - strtotime($b["date"]);
    }
    usort($all_posts, "sortFunction");

    // Here is your loop through each post
    foreach ($all_posts as $post) {
        echo $post;
    }