Query loop can’t display multiple post types
-
I have four custom post types, and am using the following code snippet (using WPCode) to display them all at the same time in one query loop:
function filter_specific_query_loop($query) {
// Check if this is an admin query or not the main query
if (is_admin() || !$query->is_main_query()) {
return;
}
// Check for the presence of the custom query parameter
if (isset($query->query_vars['custom_query_loop']) && $query->query_vars['custom_query_loop']) {
// Modify the query to include multiple post types
$query->set('post_type', array('post', 'restaurants', 'activities', 'equipment','recipes'));
}
}
add_action('pre_get_posts', 'filter_specific_query_loop');
// Add a filter to include the custom query parameter when rendering the block
function add_custom_query_var($block_content, $block) {
if ($block['blockName'] === 'core/query') {
if (isset($block['attrs']['className']) && strpos($block['attrs']['className'], 'custom-query-loop') !== false) {
add_filter('query_vars', function ($query_vars) {
$query_vars[] = 'custom_query_loop';
return $query_vars;
});
}
}
return $block_content;
}
add_filter('render_block', 'add_custom_query_var', 10, 2);I added custom class name: custom-query-loop to the query loop (located on home page). When I preview my page however, I only see one post type from the query loop.
Am I doing something wrong? My site is not live yet.
Thank you in advance for help!
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
The topic ‘Query loop can’t display multiple post types’ is closed to new replies.