Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

query posts retrieves from category and child categries WordPress

  • SOLVED

let's say my category setup is like this:

all news (id=1)
- bad news (id=2)
- good news (id=3)

i want to be able to get posts from all the subcategories as well as the category.
so they revamped version of:
<?php query_posts("cat=1&showposts=4"); ?>
<?php while (have_posts()) : the_post(); ?>


would display posts in cats 1,2,and 3

how do i do that?

Answers (4)

2010-10-19

Michael Fields answers:

That's technically how query_posts() should work. Perhaps the issue is with the 'numberposts' param? have you tried 'posts_per_page' instead?


madbadcat comments:

that's what i thought- but haven't had enough coffee and am trying to customize a themeforest theme for a non-profit. turns out the theme had a typo in the functions.php

thanks.


Michael Fields comments:

I've set up multiple sites that use categories like you are planning. One thing that I found to alleviate headaches was to never put posts in the root category ( all news ) and only assign a post to one category within the tree structure. Previous and Next navigation on single post pages can get very confusing otherwise.


madbadcat comments:

sorry
i am particularly obtuse when i have no coffee
i think i asked the wrong question
actually what's happening is that the function says this:

$categories = get_categories('hide_empty=0&orderby=name');
$wp_cats = array();
foreach ($categories as $category_list ) {
$wp_cats[$category_list->cat_ID] = $category_list->cat_name;
}
array_unshift($wp_cats, "Choose a category");


so when the index page says:
<?php query_posts("category_name=$cat_tab_2&showposts=4"); ?>

if the category name has a space in it (like news and media) the query returns nothing.

see
http://everglades.org/wp/

i thought i was tripping on what the query posts did and did not do so i focused on that but the problem seems to be about the spaces in the category name.

2010-10-19

Tobias Nyholm answers:

<?php
$categories= get_categories('child_of=1'); //the parent category
$ids="1";//the parent category
foreach($categories as $cat){
$ids.=",".$cat->ID; //I think ID is valid..
}

query_posts("cat=$ids&showposts=4");
while (have_posts()) : the_post();
?>
<!-- more stuff-->


madbadcat comments:

sorry
i am particularly obtuse when i have no coffee
i think i asked the wrong question
actually what's happening is that the function says this:

$categories = get_categories('hide_empty=0&orderby=name');
$wp_cats = array();
foreach ($categories as $category_list ) {
$wp_cats[$category_list->cat_ID] = $category_list->cat_name;
}
array_unshift($wp_cats, "Choose a category");


so when the index page says:
<?php query_posts("category_name=$cat_tab_2&showposts=4"); ?>

if the category name has a space in it (like news and media) the query returns nothing.

see
http://everglades.org/wp/

i thought i was tripping on what the query posts did and did not do so i focused on that but the problem seems to be about the spaces in the category name.

2010-10-19

Baki Goxhaj answers:

Just add the IDs of the categories.

This code:

<?php query_posts("cat=1&showposts=4"); ?>

Becomes:

<?php query_posts("<strong>cat=1,2,3</strong>&showposts=4"); ?>

2010-10-19

Denzel Chia answers:

<blockquote>if the category name has a space in it (like news and media) the query returns nothing.</blockquote>

If that is the case, which I doubt it is. You can try replacing the space with a dash.

And please do not change your question again.

Thanks.