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.

Why on earth is this piece of code not working?

    $my_query = new WP_Query('category_name=feature');
    while ($my_query->have_posts()) : $my_query->the_post();
        echo 'test';
    endwhile;

I've tried with Feature and feature, no difference. I need to get post by category name.

Any help appreciated.

update

This is not working either: $my_query = new WP_Query('cat=3');

code

// index.php

<?php
    include_once('header.php');
?>

<div id="fp-slider-container" class="blue-gradient">
    <div class="main-content-container">
        <?php
        query_posts( 'cat=3&posts_per_page=5' );

        while ( have_posts() ) : the_post();
            echo 'test';
        endwhile;


        ?>
    </div>
</div>

<?php
    include_once('footer.php');
?>
share|improve this question
    
I'll ask the obvious first: do you definitely have at least one post in that category? –  Pippin Sep 7 '12 at 21:34
    
he he, yes. I got 3 :) –  Steven Sep 7 '12 at 21:35
    
Can you provide the complete context that this query is being used in? –  Pippin Sep 7 '12 at 21:46
    
@Pippin here you go –  Steven Sep 7 '12 at 22:01
    
You do not want to use query_posts(). Does WP_Query return anything if you don't specify a category? –  Pippin Sep 7 '12 at 22:26
add comment

2 Answers

A common pitfall is the fact that:

category_name takes the category SLUG NOT the name as attribute

This is often mistaken.

share|improve this answer
add comment

instead of hacking index.php why not use the pre_get_posts filter?

add_filter('pre_get_posts', 'filter_homepage_posts');
function filter_homepage_posts($query) {
    $limit_number_of_posts = 5;
    $featured_category_id = get_cat_id('Reviews'); // by cat name...
    if ($query->is_home) {
        $query->set('cat', $featured_category_id);
        $query->set('showposts', $limit_number_of_posts);
    }
  return $query;
}

copied from: http://www.seanbehan.com/intercepting-the-wordpress-loop

should also work like this since technically the query passes by reference

add_action('pre_get_posts', 'filter_homepage_posts');
function filter_homepage_posts( $query) {
    $limit_number_of_posts = 5;
    $featured_category = 'bacon-category'; // by cat slug...
    if ($query->is_home) {
        $query->set('category_name', $featured_category);
        $query->set('showposts', $limit_number_of_posts);
    }
  return $query;
}
share|improve this answer
    
There is no hack. The index.php is whatever you make it to be, so I don't think your solution is the best solution. –  Steven Sep 7 '12 at 23:04
    
of course index.php is whatever you'd like but obliterating the query object only to make another query is inefficient. with pre_get_posts you adjust the query before it is run, so you only query the database once instead of twice with your code. unless, i misunderstood your code and this is a secondary loop? in which case you have to make a second query and i would recommend get_posts() since it always gives me less stress than with WP_query. –  helgatheviking Sep 7 '12 at 23:09
add comment

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.