WordPress Tips: Background Article List Search Support ID
On the off chance that the WordPress foundation articles are more, when investigate, the other party revealed to you the ID, in the article list can’t rapidly find the comparing article, you can utilize the accompanying code to make WordPress behind the scenes article list search support ID.
add_filter('posts_clauses', function ($clauses, $wp_query){
if($wp_query->is_main_query() && $wp_query->is_search()){
global $wpdb;
$search_term = $wp_query->query['s'];
if(is_numeric($search_term)){
$clauses['where'] = str_replace('('.$wpdb->posts.'.post_title LIKE', '('.$wpdb->posts.'.ID = '.$search_term.') OR ('.$wpdb->posts.'.post_title LIKE', $clauses['where']);
}elseif(preg_match("/^(\d+)(,\s*\d+)*\$/", $search_term)){
$clauses['where'] = str_replace('('.$wpdb->posts.'.post_title LIKE', '('.$wpdb->posts.'.ID in ('.$search_term.')) OR ('.$wpdb->posts.'.post_title LIKE', $clauses['where']);
}
}
return $clauses;
}, 2, 2);
The above code upholds looking through one article ID straightforwardly, for example, 123, and furthermore upholds various article IDs, however they should be isolated by “,”,, for example, 123,345,567
Leave a Reply