Come effettuare una ricerca WP_Query con custom post types?

13 feb 2014, 19:19:07
Visualizzazioni: 32K
Voti: 9

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?

4
Commenti

Non posso confermarlo. Ho appena testato qualcosa di molto simile - con successo. Hai provato senza alcun plugin e/o utilizzando un tema predefinito? Anche la ricerca (cioè la ricerca principale, utilizzando il modulo di ricerca) include tutti i custom post type che non sono esplicitamente esclusi dalla ricerca.

tfrommen tfrommen
13 feb 2014 20:57:56

In realtà stamattina ho scritto una ricerca che utilizzava post_type (un cpt), post_status (publish) e s e funziona perfettamente. Quasi certamente hai un filtro o qualcosa in un plugin/tema come suggerito da @tf.

Andrew Bartel Andrew Bartel
13 feb 2014 23:36:40

Non ho potuto testarlo ma sono sicuro al 95% di aver fatto qualcosa di simile e averlo fatto funzionare. Concordo con la speculazione sull'interferenza di azioni o filtri.

s_ha_dum s_ha_dum
14 feb 2014 00:02:32

Hai ragione, ho provato in un'installazione pulita di Wordpress e funziona. Sembra che uno dei plugin che uso nel mio ambiente si stia agganciando a uno degli eventi pre-query. Grazie!

Sebastian Sebastian
17 feb 2014 16:57:03
Tutte le risposte alla domanda 4
1

Hai ragione, ho provato in un'installazione pulita di WordPress e funziona.

Sembra che uno dei plugin che utilizzo nel mio ambiente si stia agganciando in uno degli eventi pre-query.

Grazie!

17 feb 2014 17:13:47
Commenti

Sai quale plugin ha causato questo problema?

stefano1 stefano1
9 ott 2014 19:21:23
0

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;
}
29 apr 2015 08:57:24
0

Ho appena incontrato questo problema e la soluzione che ha funzionato per me è stata:

invece di 'post_type' => 'node' prova 'post_type' => ['node']

18 gen 2023 21:44:23
0
-2

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.

5 set 2018 12:04:08