I want to have to display related post within a post based on the post title. So for example I have a post named "France gas prices". First it displays all the post info but then under the post I want to have search results displayed using the "france gas prices" value. Now I could use this. (example search on specific meta data but i would be happy if it just searched post title)

<?php $title = get_the_title() ?>
<?php query_posts('meta_key=review_type&meta_value=$title');  ?> 
<?php if (have_posts()) : ?> 
<?php while (have_posts()) : the_post(); ?>

And then display the info from the found posts as I want but the problem is then I will only find exact matches. It wont find gas prices france or any other result like wordpress build in search would give.

How can I access the build in wordpress search give it a term dynamicly based on post title and then choose what I display from the found posts and how I display the info?

---> in other words how do you the build in wordpress functionality to fill a wp_query?

1 Answer 1


WP_Query accepts a search parameter, 's'. Hence, you can just do this:

<?php $related_posts = new WP_Query( 's=' . the_title_attribute( '', '', 0 ) );

if( $related_posts->have_posts() ) : while( $related_posts->have_posts() ) : $related_posts->the_post();

    // STUFF

endwhile; endif; wp_reset_postdata(); ?>

While we're here, note the use of WP_Query which is more appropriate for a secondary loop. You can also take a look at the link above if you want to add additional paramemters to restrict by post_type or taxonomy or anything else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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