query_post by title?

14

¿Es posible crear un bucle de publicaciones usando WP_Query o query_posts usando el título?

es decir

$args = array('post_title'='LIKE '.$str.'% ');

$res = WP_Query($arg);

// the loop...


// trying this now...
$mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title like 'Abb%' ");

echo count($mypostids).", ";    // works but can't echo out array of IDs for the next args?

$args = array(
    'post__in'=> $mypostids
);

$res = WP_Query($args);

while( $res->have_posts() ) : $res->the_post(); ...
    
pregunta v3nt 14.07.2011 - 16:16

5 respuestas

3

conseguí este trabajo con la ayuda de esta publicación al final. Saludos chicos;

$finalArgs =  array (       
        'posts_per_page'=>5,
        'order' => 'ASC',
        'post_type' => 'school'                         
    );

    // Create a new instance
    $searchSchools = new WP_Query( $finalArgs );

    $mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title LIKE '".$str."%' ");

    $args = array(
        'post__in'=> $mypostids,
        'post_type'=>'school',
        'orderby'=>'title',
        'order'=>'asc'
    );

    $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;
    
respondido por el v3nt 19.07.2011 - 15:04
27

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);
    
respondido por el Brady 14.07.2011 - 17:21
2

Obtén 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();

    /* Loop here */

endwhile; endif; 

wp_reset_query(); ?>
    
respondido por el Zakir Sajib 23.11.2011 - 00:32
0

Sí, es posible ....

global $wpdb;

$mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title like '%$str%' ");

$args = array('post__in'=$mypostids);

$res = WP_Query($arg);
    
respondido por el Rajeev Vyas 14.07.2011 - 16:29
0

Estas respuestas me parecen que intentan piratear wordpress.

Consulte la misma pregunta sobre el desbordamiento de pila:

enlace

Esto funciona si desea hacer 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'
  ) 
);

Esta consulta de ejemplo es para un tipo de publicación llamado relojes y la 's' (término de búsqueda) es donde puede buscar los títulos de sus publicaciones en la consulta

    
respondido por el gtamborero 16.11.2018 - 12:44

Lea otras preguntas en las etiquetas