post per page for differents post types in a same query

I have a feeling there are multiple ways to accomplish what you want, but here is one option for consideration.

Option 1

You should be able to achieve this by using the WP_Query class in WordPress. To query multiple post types and define the number of posts for each one, you can use the following code:

// Define an array to map post types to the number of posts per type
$post_types_counts = array(
    'infografias' => 4,               // Number of posts for 'infografias'
    'multimedia-sistemas' => 6,      // Number of posts for 'multimedia-sistemas'
    'descargables' => 2             // Number of posts for 'descargables'
);

$query_args = array(
    'post_type' => array_keys($post_types_counts), // Get the post types from the array keys
    'post_status' => 'publish',
    'order' => 'ASC',
    'orderby' => 'date',
    'paged' => '1',
    'meta_query' => array(
        array(
            'key' => 'pais',
            'value' => $pais
        )
    )
);

// Create a new WP_Query instance
$custom_query = new WP_Query($query_args);

// Loop through the results
if ($custom_query->have_posts()) {
    while ($custom_query->have_posts()) {
        $custom_query->the_post();
        $post_type = get_post_type(); // Get the post type of the current post

        // Check if the post type exists in the mapping array
        if (isset($post_types_counts[$post_type]) && $post_types_counts[$post_type] > 0) {
            // Display your post content here

            // Decrease the count for the current post type
            $post_types_counts[$post_type]--;
        }
    }
}

// Restore the global post data
wp_reset_postdata();

In this code:

  1. We use the $post_types_counts array to map post types to the number of posts you want to display for each type.
  2. The main query includes all specified post types but doesn’t specify the number of posts per page for each type.
  3. Inside the loop, we check if the current post type exists in the mapping array and has remaining posts to display. If so, we display the post content and decrease the count for that post type.
  4. This approach allows you to set the desired number of posts for each post type in a more organized way.

Option 2

You can achieve the same result by using the pre_get_posts action hook to modify the main query before it is executed. In this approach, you would execute multiple separate WP_Query instances for each post type and then merge the results into a single query. Here’s the code for this alternate option:

// Define an array to map post types to the number of posts per type
$post_types_counts = array(
    'infografias' => 4,               // Number of posts for 'infografias'
    'multimedia-sistemas' => 6,      // Number of posts for 'multimedia-sistemas'
    'descargables' => 2             // Number of posts for 'descargables'
);

function customize_main_query($query) {
    if ($query->is_main_query() && !is_admin()) {
        // Define an array to store the results of separate WP_Query instances
        $merged_posts = array();

        // Loop through the post types and execute separate WP_Query instances
        foreach ($post_types_counts as $post_type => $count) {
            $args = array(
                'post_type' => $post_type,
                'post_status' => 'publish',
                'order' => 'ASC',
                'orderby' => 'date',
                'posts_per_page' => $count,
                'meta_query' => array(
                    array(
                        'key' => 'pais',
                        'value' => $pais
                    )
                )
            );

            $custom_query = new WP_Query($args);

            // Merge the results into the $merged_posts array
            $merged_posts = array_merge($merged_posts, $custom_query->posts);
        }

        // Sort the merged posts by date if needed
        usort($merged_posts, function($a, $b) {
            return strtotime($a->post_date) - strtotime($b->post_date);
        });

        // Replace the main query results with the merged posts
        $query->posts = $merged_posts;
        $query->post_count = count($merged_posts);
    }
}

// Hook into the pre_get_posts action to customize the main query
add_action('pre_get_posts', 'customize_main_query');

In this code:

  1. We define a function customize_main_query that hooks into pre_get_posts to modify the main query.
  2. Inside the function, we execute separate WP_Query instances for each post type with the specified parameters and store the results in the $merged_posts array.
  3. After merging the results, we sort the merged posts by date if needed.
  4. Finally, we replace the main query results with the merged posts, effectively combining the separate queries into a single revised query.

This alternate option allows you to achieve the desired result using pre_get_posts and separate WP_Query instances.

deneme bonusudeneme bonusu veren sitelerpulibet girişOnwin Güncel Giriştürkçe altyazılı pornocanlı bahis casino