Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

is it possible to create a loop of posts using WP_Query or query_posts using the title?

ie

$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(); ...
share|improve this question

5 Answers 5

Yes it is possible....

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);
share|improve this answer
    
cheers Rajeev - tried this but it gets stuck after the get_col. I've updated the above^^^ –  daniel Crabbe Jul 14 '11 at 15:17

I would do it this way:

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;
}
?>

Then:

$args = array(
    'post_title_like' => $str
);
$res = WP_Query($arg);
share|improve this answer
    
Works great except for the second argument (the referenced $wp_query), which doesn't seem to be part of the filter callback (see codex.wordpress.org/Plugin_API/Filter_Reference/posts_where) and will generate an error. –  maryisdead Oct 14 '13 at 12:37
    
actually, the code is correct as is, at least in WP 4.1.1. It's the codex that's omitting the second argument, so if you declare 2 arguments in add_filter as in the example, it works fine. Another advantage of this solution is that it works for custom post types. –  yitwail Mar 22 at 4:49
    
This should be accepted answer since this shows how to do it using WordPress built-in functions and not using custom SQL query. –  Sudar Apr 2 at 6:11
up vote 2 down vote accepted

got this working with the help from this post in the end. Cheers guys;

$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;
share|improve this answer

Get the title from another loop

$title = get_the_title();

and use the $title variable if you wish.

<?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(); ?>
share|improve this answer
    
it works on custom post type and wordpress 3.2.1 –  Zakir Sajib Nov 22 '11 at 23:35
    
Thanks, the exact hack I need =) –  realmag777 Jul 28 '14 at 19:42
// The Query
$the_query = new WP_Query( 'orderby' => 'title' );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.