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.

The two functions below are designed to return a list of posts that share the same category as the current post being viewed in single.php

However, when I var_dump on $cat (should be a list of the categories assigned to the current post), I'm not getting what I'm expecting. Do I need to deserialize the array before passing it to the get_posts query?

function get_cats()
{
$post_cats= array();
$categories = get_the_category();
foreach($categories as $cat){
array_push($post_cats, $cat->cat_ID);
}
return $post_cats;
}

//get related posts by category
function ce4_get_related_by_category()
{
global $post;
$cat = implode(',',get_cats());
$catHidden=get_cat_ID('hidden');
$myqueryCurrent = new WP_Query();
$myqueryCurrent->query(array('cat' => "$cat,-$catHidden",'post__not_in' => get_option('sticky_posts')));
$totalpostcount = $myqueryCurrent->found_posts;
if($totalpostcount > 0)
    {
        echo "<ul>";
        $myposts = get_posts(array('cat' => "$cat,-$catHidden",'numberposts' => $cb2_current_count));
        foreach($myposts as $idx=>$post) 
        {
        ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><?php the_excerpt(); ?></li>
        <?php 
        }
        echo "</ul>";
    }
} 
share|improve this question

2 Answers 2

up vote 2 down vote accepted

You can use get_the_category()

It will return an array of category IDs belonging to the current post.


$post_cats= array();
$categories = get_the_category();
foreach($categories as $cat) :
array_push($post_cats, $cat->cat_id);
endforeach;

Then the $post_cats array will have a list of all the ids.

share|improve this answer
    
When I var_dump(get_the_category($post->ID)); it appears as if I'm getting the entire category objects. Can I pass an argument to limit the response to a list of cat_id's? –  Scott B Jan 19 '11 at 15:40
    
You can't limit the response to only ids, but you could try something like this to get an array of only the IDs. (check my answer, edited) –  Pippin Jan 19 '11 at 15:43
    
Thanks Pippin, I've updated my question with your suggestions. I'm getting array(1) { [0]=> NULL } when I var_dump($cat). I'm expecting '10,7' –  Scott B Jan 19 '11 at 16:00
    
Should be $cat->cat_ID in Loop. –  Rarst Jan 19 '11 at 16:13

You can fetch array of IDs alone like this with wp_get_object_terms():

wp_get_object_terms($id, 'category', array('fields' => 'ids'))

Note that get_the_category() caches results and getting IDs from its return might actually be more efficient than fetching IDs separately with this.

share|improve this answer
    
Thanks Rarst, do I need to deserialize the array before passing it to $myqueryCurrent and $myposts? –  Scott B Jan 19 '11 at 16:24
    
@Scott B according to your current code you will need to loop through array to negate values and add them to string. I think it would be simpler to use category__not_in instead of cat and just pass array of IDs. –  Rarst Jan 19 '11 at 16:27
    
I've edited the code. I just needed to run the implode method on the variable. It seems to work. I just need to remove the current post from the output loop. –  Scott B Jan 19 '11 at 16:36
    
@Scott B ah, I misunderstood there slightly, thought you want to exclude these rather than include. Yep, in such case implode will do just fine. –  Rarst Jan 19 '11 at 16:38

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.