0

How can I make a query and in the same query query many types of publications and also define the number of publications for each one.?

This is what I have in my query

$post_types = array('infografias', 'multimedia-sistemas', 'descargables');
$query_args = array(
'post_type' => $post_types,
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'date',
'posts_per_page' => '12',
'offset' => '0',
'paged' => '1',
'meta_query' => array(
  array(
    'key' => 'pais',
    'value' => $pais
  )
),

I appreciate your help.

1
  • If you want more than the defined "12" on any of the types, you'll need to split it into a separate query. You could increase the posts_per_page to whatever type has the "max" posts per page, and then just terminate the loop per type at the desired limit.
    – Howdy_McGee
    Commented Dec 21, 2023 at 16:22

1 Answer 1

0

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.