Problema con get_posts e meta_compare='LIKE' in WordPress
Sto utilizzando il seguente codice:
$tolettpe = "Sale";//valore predefinito
if($_REQUEST['tolettype']) $tolettpe = $_REQUEST['tolettype'];
else if($_REQUEST['srch_type']) $tolettpe = $_REQUEST['srch_type'];
$args = array(
'numberposts' => $latestcount,
'category' => $catidstr,
'meta_key' => 'property_type',
'meta_compare' => 'LIKE',
'meta_value' => $tolettpe.'%'
);
$post_content = get_posts($args);
Il valore nel database è 'Sale||' e non ci sono variabili nella stringa di query della richiesta.
Ma la query non restituisce alcun risultato.
Se utilizzo il valore esatto e nessun meta_compare, funziona.
Qualche idea su come farlo funzionare?

meta_compare
I valori possibili sono '!=', '>', '>=', '<', or '<='
. Il valore predefinito è '='
Se vuoi utilizzare LIKE
devi creare una meta_query ad esempio:
$tolettpe = "Vendita";//valore predefinito
if($_REQUEST['tolettype']) $tolettpe = $_REQUEST['tolettype'];
else if($_REQUEST['srch_type']) $tolettpe = $_REQUEST['srch_type'];
$args = array(
'numberposts' => $latestcount,
'category' => $catidstr,
'meta_query' => array(
array(
'key' => 'property_type',
'value' => $tolettpe,
'compare' => 'LIKE'
)
)
);
$posts = get_posts($args);
La query generata inserisce il termine di ricerca tra due segni di %, quindi non è necessario aggiungerli nel codice.

grazie, ho provato ma non ottengo alcun risultato. C'è un modo per stampare la query generata così posso eseguirla direttamente sul database per debug?

ok, ho trovato la risposta qui http://codex.wordpress.org/Editing_wp-config.php#Save_queries_for_analysis
