1

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>";
    }
} 

2 Answers 2

2

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.

4
  • 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
    Commented Jan 19, 2011 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
    Commented Jan 19, 2011 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
    Commented Jan 19, 2011 at 16:00
  • Should be $cat->cat_ID in Loop.
    – Rarst
    Commented Jan 19, 2011 at 16:13
1

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.

4
  • Thanks Rarst, do I need to deserialize the array before passing it to $myqueryCurrent and $myposts?
    – Scott B
    Commented Jan 19, 2011 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
    Commented Jan 19, 2011 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
    Commented Jan 19, 2011 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
    Commented Jan 19, 2011 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.