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(); ...
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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 = new WP_Query($args);
Method 2
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;
Method 3
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(); ?>
Method 4
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);
Method 5
These replies seem to me as trying to hack wordpress.
Refer to the same question on stack overflow:
https://stackoverflow.com/questions/25761593/wp-query-with-post-title-like-something-and-category
This works if you want to do a search query by title ordered by title:
$the_query = new WP_Query(
array(
'post_type' => 'watches',
'posts_per_page' => 5,
'orderby' => 'title',
's' => 'my title'
)
);
This example query is for a post type named watches and the ‘s’ (search term) is where you can look for your post titles on the query
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0