Modificar las búsquedas en WordPress
Si queremos modificar las búsquedas que ofrece WordPress para que devuelva lo que nosotros queremos tan solo hay que hacer dos funciones para dos filtros:
add_filter('posts_results', 'mi_search');
function mi_search($posts) {
$s = get_query_var('s');
if (condicion($s)) {
$posts = array();
$paged = intval(get_query_var('paged'));
if ($paged < 1) $paged = 1;
$posts_per_page = intval(get_query_var('posts_per_page'));
$from = ($paged-1)*$posts_per_page;
global $wpdb;
$_posts = $wpdb->get_results("select post_id from $wpdb->posts where loquesea order by post_id desc limit $from, $posts_per_page");
foreach($_posts as $p) {
$posts[] = get_post($p->post_id);
}
}
return $posts;
}
add_filter('found_posts', 'mi_found_posts');
function mi_found_posts($n) {
$s = get_query_var('s');
if (condicion($s)) {
global $wpdb;
$res = $wpdb->get_results("select count(*) as n from $wpdb->posts where condicion ");
$n = $res[0]->n;
}
return $n;
}