¿Cómo consultar posts por título en WordPress?
¿Es posible crear un bucle de posts usando WP_Query o query_posts utilizando el título?
Ejemplo:
$args = array('post_title'='LIKE '.$str.'% ');
$res = WP_Query($arg);
// el bucle...
// probando esto ahora...
$mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title like 'Abb%' ");
echo count($mypostids).", "; // funciona pero no puedo mostrar el array de IDs para los siguientes 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;
}
?>
Luego:
$args = array(
'post_title_like' => $str
);
$res = new WP_Query($args);

Funciona muy bien excepto por el segundo argumento (la referencia a $wp_query
), que parece no ser parte del callback del filtro (ver http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where) y generará un error.

en realidad, el código es correcto tal como está, al menos en WP 4.1.1. Es el codex el que omite el segundo argumento, así que si declaras 2 argumentos en add_filter
como en el ejemplo, funciona bien. Otra ventaja de esta solución es que funciona para custom post types.

Esta debería ser la respuesta aceptada ya que muestra cómo hacerlo usando las funciones nativas de WordPress y no usando una consulta SQL personalizada.

parece que falta el primer %
aquí. Justo después de LIKE \'
. Lo agregué y comenzó a funcionar (4.2.4)

Sí, falta el primer %
, también lo agregué y funciona :) (¿quizás debería editar la respuesta?)

Finalmente logré que funcionara con la ayuda de este post. ¡Gracias chicos!
$finalArgs = array (
'posts_per_page'=>5, // Número máximo de posts por página
'order' => 'ASC', // Orden ascendente
'post_type' => 'school' // Tipo de post personalizado
);
// Crear una nueva instancia de WP_Query
$searchSchools = new WP_Query( $finalArgs );
// Obtener IDs de posts donde el título comienza con la cadena buscada
$mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title LIKE '".$str."%' ");
$args = array(
'post__in'=> $mypostids, // Solo posts con estos IDs
'post_type'=>'school', // Tipo de post
'orderby'=>'title', // Ordenar por título
'order'=>'asc' // Orden ascendente
);
$res = new WP_Query($args);
// Loop a través de los resultados
while( $res->have_posts() ) : $res->the_post();
global $post;
// Obtener metadato del post
$EstablishmentNumber = get_post_meta($post->ID,'EstablishmentNumber', true);
// Crear array con datos de la escuela
$schl = array(
'id'=>$EstablishmentNumber,
'label'=>$post->post_title,
'value'=>$EstablishmentNumber
);
$matchedSchools[] = $schl;
endwhile;

Obtener el título de otro bucle
$title = get_the_title();
y usa la variable $title si lo deseas.
<?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();
/* Bucle aquí */
endwhile; endif;
wp_reset_query(); ?>

Sí, es posible...
global $wpdb;
// Obtener IDs de posts cuyo título coincida con el string buscado
$mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title like '%$str%' ");
// Configurar argumentos para la consulta WP_Query
$args = array('post__in' => $mypostids);
// Ejecutar la consulta
$res = WP_Query($args);

Estas respuestas me parecen intentos de hackear WordPress.
Consulta la misma pregunta en Stack Overflow:
https://stackoverflow.com/questions/25761593/wp-query-with-post-title-like-something-and-category
Esto funciona si deseas realizar una consulta de búsqueda por título ordenada por título:
$the_query = new WP_Query(
array(
'post_type' => 'watches',
'posts_per_page' => 5,
'orderby' => 'title',
's' => 'my title'
)
);
Este ejemplo de consulta es para un tipo de publicación llamado 'watches' y el 's' (término de búsqueda) es donde puedes buscar los títulos de tus publicaciones en la consulta.
