John Davidson

php - Limit search results per CPT (wordpress)

0 comments
Message:


Im currently building a search filter. But my problem is that I can't figure out how to limit search results per custom post type. The search function:


function ledvionSearchResults($data) { 
$mainQuery = new WP_Query(array(
'post_type' => array('post', 'page', 'category', 'product'),
'posts_per_page' => -1,
's' => sanitize_text_field($data['term'])
));

$results = array(
'products' => array(),
'categories' => array(),
'generalInfo' => array()
);

while($mainQuery->have_posts()) {
$mainQuery->the_post();

if (get_post_type() == 'product') {
array_push($results['products'], array(
'title' => get_the_title(),
'permalink' => get_the_permalink(),
'image' => get_the_post_thumbnail_url(),
'ean' => get_field('barcode'),
'sku' => get_field('sku')
));
}

if (get_post_type() == 'category') {
array_push($results['categories'], array(
'title' => get_the_title(),
'permalink' => get_the_permalink()
));
}

if (get_post_type() == 'post' OR get_post_type() == 'page') {
array_push($results['generalInfo'], array(
'title' => get_the_title(),
'permalink' => get_the_permalink()
));
}
}

return $results;

}

I have tried to merge multiple queries but that didnt seem to work and I tried the pre_get_posts action but that would only work for one or all custom post types. Example below:


function ledvion_adjust_queries($query){
if (!is_admin() && $query->is_search && !is_post_type_archive('product')) {
$query->set('post_type', array('product'));
$query->set('posts_per_page', 5);
$query->set('orderby', 'title');
$query->set('order', 'ASC');
}

return $query;
}
add_action('pre_get_posts', 'ledvion_adjust_queries');


My Answer:
To limit search results per custom post type, you can modify the mainQuery in your search function to include the 'post_type' parameter for each custom post type and set the 'posts_per_page' parameter accordingly. Here's an example of how you can modify your search function to limit search results per custom post type:

php
function ledvionSearchResults($data) {
$results = array(
'products' => array(),
'categories' => array(),
'generalInfo' => array()
);

$productQuery = new WP_Query(array(
'post_type' => 'product',
'posts_per_page' => 5, // Limit to 5 products
's' => sanitize_text_field($data['term'])
));

while($productQuery->have_posts()) {
$productQuery->the_post();

array_push($results['products'], array(
'title' => get_the_title(),
'permalink' => get_the_permalink(),
'image' => get_the_post_thumbnail_url(),
'ean' => get_field('barcode'),
'sku' => get_field('sku')
));
}

// Repeat the above process for other custom post types

return $results;
}


In this modified function, I've added separate WP_Query instances for each custom post type (e.g., 'product', 'category', 'post', 'page') with their respective 'posts_per_page' limits. You can adjust the 'posts_per_page' value for each custom post type as needed.

This way, you can limit the search results per custom post type in your search filter.

Rate this post

5 of 5 based on 9317 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us