Filters are used to modify data on specific events. You can create functions based on the filters list below with the available parameters. Filters are a great way of modifying results without touching the code.
Usage examples
Here you can see two basic examples of filter usage. One is adding , and second is a simple .
For more examples, check the .
/**
* Add category titles to result titles
*
* @link: https://wp-dreams.com/knowledge-base/showing-the-category-titles-in-the-result-title/
*/
add_filter( 'asp_pagepost_results', 'asp_add_category_titles', 1, 1 );
function asp_add_category_titles( $pageposts ) {
foreach ($pageposts as $k=>$v) {
// Get the post categories
$post_categories = wp_get_post_categories( $pageposts[$k]->id );
$cats = "";
// Concatenate category names to the $cats variable
foreach($post_categories as $c){
$cat = get_category( $c );
$cats = " ".$cat->name;
}
// Modify the post title
$pageposts[$k]->title .= " ".$cats;
}
return $pageposts;
}
/**
* Numbering the results
*
* @link: https://wp-dreams.com/knowledge-base/numbering-the-results/
*/
add_filter( 'asp_results', 'asp_number_results', 1, 1 );
function asp_number_results( $results ) {
var $num = 1;
foreach ($results as $k=>$v) {
// Modify the post title
$results[$k]->title = $num . ". " . $results[$k]->title;
$num++;
}
return $results;
}