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.

How do i add the current post/page title in a post query?

I've tried this, doesn't work:

query_posts( 'posts_per_page=10category_name=&' . $post->post_title); if (have_posts()) : while (have_posts()) : the_post(); 
share|improve this question
1  
what are you trying to query for exactly? –  Milo Jul 3 '12 at 16:01
    
do you mean $post->post_title = category –  gowri Jul 3 '12 at 16:21
    
I want the category_name to be the page title –  DennisT Jul 3 '12 at 19:22
    
I want to query a specific category, the category being the same as page title. –  DennisT Jul 3 '12 at 19:22

2 Answers 2

up vote 2 down vote accepted

Your original code has a typo - the ampersand is used to separate arguments, so it should be:

'posts_per_page=10&category_name=' . $post->post_title

However, since this is a new query in addition to the default, a new instance of WP_Query should be used instead of query_posts:

$args = array(
    'posts_per_page' => 10,
    'category_name' => $post->post_title
);

$related = new WP_Query( $args );

if( $related->have_posts() ):
    while( $related->have_posts() ):
        $related->the_post();
    endwhile;
endif;
share|improve this answer
    
Exactly what I was looking for! Big thanks Milo! –  DennisT Jul 4 '12 at 7:45

To others who wants to use this,

This solution makes a page into a category page, an easy way to get categories into a header navigation that lists pages.

Just make this code into a page template and set a title that matches the category you want to show.

<?php 

$args = array('posts_per_page' => 10, 'category_name' => $post->post_title );

$related = new WP_Query( $args );

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

?>

<?php the_title(); ?>
<?php the_content(); ?>


<?php endwhile; endif; ?>
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.