Come effettuare una ricerca WP_Query con custom post types?
Ho registrato un custom post type chiamato "node".
Quando creo una WP_Query base per ottenere i post di quel tipo, funziona perfettamente. Esempio:
$args = array(
'post_status' => 'publish',
'post_type' => 'node');
$query = new WP_Query($args);
Questo recupererà tutti i post pubblicati di tipo "node".
Tuttavia, non appena combino questo con una ricerca, non viene restituito nulla. Esempio:
$args = array(
'post_status' => 'publish',
'post_type' => 'node',
's' => 'Il mio termine di ricerca');
$query = new WP_Query($args);
Questo non restituirà nulla, anche se dovrebbe trovare diversi post di tipo 'node' che contengono 'Il mio termine di ricerca'.
Per quanto posso vedere, i post type vengono automaticamente impostati su "post" e "page" non appena includo il parametro "s" in $args. Se stampo un var_dump di $query, mostra quanto segue:
Senza "s":
object(WP_Query)
public 'query' =>
array (size=2)
'post_status' => string 'publish' (length=7)
'post_type' => string 'node' (length=4)
public 'query_vars' =>
array (size=63)
'post_status' => string 'publish' (length=7)
'post_type' => string 'node' (length=4)
...
Con "s":
object(WP_Query)
public 'query' =>
array (size=2)
'post_status' => string 'publish' (length=7)
's' => string 'Il mio termine di ricerca' (length=14)
'post_type' => string 'node' (length=4)
public 'query_vars' =>
array (size=66)
'post_status' => string 'publish' (length=7)
's' => string 'Il mio termine di ricerca' (length=14)
'post_type' =>
array (size=2)
0 => string 'post' (length=4)
1 => string 'page' (length=4)
...
Quindi WordPress sembra sovrascrivere i post type non appena viene coinvolta una ricerca.
Come posso risolvere questo problema?

In un'installazione standard di WordPress, WP_Query avrà come default post_type
= 'post'
.
Considera questo semplice esempio:
$args = array(
"posts_per_page"=>5,
"ignore_sticky_posts"=>true
);
$itposts = new WP_Query( $args );
Risulterà nella seguente query eseguita:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY wp_posts.post_date DESC LIMIT 0, 5
LA SOLUZIONE MIGLIORE
La soluzione è aggiungere un argomento post_type
impostato su 'any'
. In questo modo WordPress cercherà automaticamente tra tutti i tipi tranne le revisioni e i tipi con exclude_from_search
impostato su true
. Il problema è che, per default, gli allegati non sono esclusi dalla ricerca. Fintanto che non hai un argomento post_status
impostato su 'inherit'
, non dovresti avere problemi perché gli allegati hanno sempre post_status
impostato su 'inherit'
.
In questo modo la query cambierà in:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type IN ('post', 'page', 'attachment') AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') ORDER BY wp_posts.post_date DESC LIMIT 0, 5
Se necessario, puoi escludere gli allegati dalla ricerca utilizzando questo codice:
add_action('init', 'exclude_attachments_from_search');
function exclude_attachments_from_search() {
global $wp_post_types;
$wp_post_types['attachment']->exclude_from_search = true;
}

So che questa è una domanda molto vecchia, ma mi sono imbattuto in questo perché avevo lo stesso problema. Non posso commentare a causa della reputazione, ma voglio aiutare persone come @Stefano in modo che non dobbiate passare in rassegna tutti i vostri plugin per verificare quale causi la modifica alla query.
Nel mio caso si trattava di un plugin chiamato "Custom Search by BestWebSoft". Ma non era colpa del plugin, semplicemente non avevo spuntato la casella per il mio custom post type nelle impostazioni.
