query_post per titolo?
È possibile creare un loop di post usando WP_Query o query_posts filtrando per titolo?
ad esempio
// Definizione degli argomenti per la ricerca per titolo
$args = array('post_title'='LIKE '.$str.'% ');
$res = WP_Query($arg);
// il loop...
// Sto provando questo adesso...
$mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title like 'Abb%' ");
echo count($mypostids).", "; // funziona ma non riesco a mostrare l'array di ID per i successivi args?
$args = array(
'post__in'=> $mypostids
);
$res = WP_Query($args);
while( $res->have_posts() ) : $res->the_post(); ...

functions.php
<?php
add_filter( 'posts_where', 'title_like_posts_where', 10, 2 );
function title_like_posts_where( $where, $wp_query ) {
global $wpdb;
if ( $post_title_like = $wp_query->get( 'post_title_like' ) ) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( $wpdb->esc_like( $post_title_like ) ) . '%\'';
}
return $where;
}
?>
Poi:
$args = array(
'post_title_like' => $str
);
$res = new WP_Query($args);

Funziona benissimo tranne che per il secondo argomento (il $wp_query
referenziato), che non sembra far parte del callback del filtro (vedi http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where) e genererà un errore.

in realtà, il codice è corretto così com'è, almeno in WP 4.1.1. È il codex che omette il secondo argomento, quindi se dichiari 2 argomenti in add_filter
come nell'esempio, funziona perfettamente. Un altro vantaggio di questa soluzione è che funziona per i custom post type.

Questa dovrebbe essere la risposta accettata poiché mostra come farlo utilizzando le funzioni integrate di WordPress e non usando query SQL personalizzate.

sembra che il primo %
manchi qui. Subito dopo LIKE \'
. L'ho aggiunto e ha iniziato a funzionare (4.2.4)

Sì, manca il primo %
, l'ho aggiunto anch'io e funziona :) (forse bisognerebbe modificare la risposta?)

alla fine ho risolto con l'aiuto di questo post. Grazie ragazzi;
$finalArgs = array (
'posts_per_page'=>5, // Numero di post per pagina
'order' => 'ASC', // Ordine crescente
'post_type' => 'school' // Tipo di post
);
// Crea una nuova istanza
$searchSchools = new WP_Query( $finalArgs );
$mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title LIKE '".$str."%' ");
$args = array(
'post__in'=> $mypostids, // ID dei post da includere
'post_type'=>'school', // Tipo di post
'orderby'=>'title', // Ordina per titolo
'order'=>'asc' // Ordine crescente
);
$res = new WP_Query($args);
while( $res->have_posts() ) : $res->the_post();
global $post;
$EstablishmentNumber = get_post_meta($post->ID,'EstablishmentNumber', true);
$schl = array('id'=>$EstablishmentNumber, 'label'=>$post->post_title , 'value'=>$EstablishmentNumber );
$matchedSchools[] = $schl;
endwhile;

Ottieni il titolo da un altro loop
$title = get_the_title();
e utilizza la variabile $title se lo desideri.
<?php
global $post, $current_post_id, $title;
function filter_where($where = ''){
global $title;
$where .= "AND post_title = '$title'";
return $where;
}
add_filter('posts_where', 'filter_where');
$query = new WP_Query(array('post_type' => 'sessions') );
if ( have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
/* Loop qui */
endwhile; endif;
wp_reset_query(); ?>

Sì, è possibile...
global $wpdb;
// Recupera gli ID dei post che contengono la stringa cercata nel titolo
$mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title like '%$str%' ");
// Crea l'array di argomenti per la query
$args = array('post__in' => $mypostids);
// Esegue la query
$res = WP_Query($arg);

Queste risposte mi sembrano tentativi di hackerare WordPress.
Fai riferimento alla stessa domanda su Stack Overflow:
https://stackoverflow.com/questions/25761593/wp-query-with-post-title-like-something-and-category
Questo funziona se vuoi eseguire una query di ricerca per titolo ordinata per titolo:
$the_query = new WP_Query(
array(
'post_type' => 'watches',
'posts_per_page' => 5,
'orderby' => 'title',
's' => 'my title'
)
);
Questa query di esempio è per un post type chiamato "watches" e il parametro 's' (termine di ricerca) è dove puoi cercare i titoli dei post nella query.
