• Hello all,

    I am using query_posts() to retrieve some posts in a specific category. My code is like this:

    $posts = query_posts("name=$name&category_name=$category_slug");

    But the problem is it seems like the category_name parameter is ignored, just the name is looked at

    Eg, I have a post with the name of “My Post” and its category is “cat1”; I also have another category which is “cat2”. When I tried the code below, I guessed the post should NOT be retrieved, but it was still included in the result:

    $name = 'My Post';
    $category_slug = 'cat2'; // yes, 'cat2' for not including 'My Post'
    $posts = query_posts("name=$name&category_name=$category_slug");

    Please give me any clue. Thank you all so much!

Viewing 3 replies - 1 through 3 (of 3 total)
  • I am using query_posts() to retrieve some posts in a specific category.

    why are you using ‘name’ and ‘category_name’ at the same time?

    try:
    $posts = query_posts("category_name=$category_slug");

    Thread Starter julienarchille

    (@julienarchille)

    why are you using ‘name’ and ‘category_name’ at the same time?

    @alchymyth. Sorry for the misleading explanation

    I was trying to get a specific post within a specific category

    Saying, I had 2 posts with the same name of "My Post" (ID = 1 and ID = 2) which is respectively belong to 2 categories cat1 and cat2. I was trying to get the post "My Post" in the cat1. That’s why my code was like above

    So, what should I do? Thank you!

    Hi,
    I have a similar problem. i want to have 0 posts in some specific category pages.
    So I used alternatevely the following codes:

    1) in functions.php
    if(!is_category(array( ‘cat ID or name’ ))) {
    query_posts(‘posts_per_page=0’);
    2) in archive php
    <?php if(!is_category(array( ‘cat ID or name’ ))) : ?>
    <?php itx_loop();?>
    <?php endif; ?>

    The problem is that in both cases also the category page title and all my widgets disappear: I’d like them to still be there!
    Please let me know what i should to…

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘query_posts with specific category_name but all posts are retrieved’ is closed to new replies.