• Resolved stevechatterton

    (@stevechatterton)


    This week’s project is to figure out a way to pass a list of a post’s categories to a function in order to pass back some related posts that may also be of interest to a reader. I’m using Exec-PHP plug-in to do stuff like this from within a post.

    I think I’ve got it down that I could this manually building an array, but I’d prefer if there was a pre-existing function that I’m currently not aware of that I could hit.

    Any advice?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter stevechatterton

    (@stevechatterton)

    I haven’t, but I’m going to try to do this without upping my overhead. That, and the fact that I’m so darn close.

    wp_get_post_categories($post->ID) will return an array of the post’s category ids;
    works in single.php, no idea if it will work from within a post.

    Thread Starter stevechatterton

    (@stevechatterton)

    alchymyth – that doesn’t seem to be working. It seems to be passing every category, not just the post specific ones.

    Here’s my call from within the post:

    <?php 
    
    $bookID = $post->ID;
    $inCats =  wp_get_post_categories($post->ID);
    // alchymyth's suggestion
    
    listRelated($inCats, $bookID);
    
    ?>

    And here’s my function:

    function listRelated($inCats, $bookID) {
    
    	$args = array(
    		 	'category__in' => $inCats,
    			'post__not_in' => $bookID,
    			'orderby' => 'modified',
    			'order' => 'DESC',
    			'post_type' => 'post',
    			'post_status' => 'publish',
    			'posts_per_page' => 5,
    			'caller_get_posts'=> 1
    		 );	
    
    			$my_query1 = new WP_Query($args);
    				while ($my_query1->have_posts()) : $my_query1->the_post();
    				$do_not_duplicate1 = $post->ID; ...

    Any other ideas how to auto-pass the categories?

    i tried echoing the varibles, and ‘$post->ID’ does not seem to hold the post’s id;
    (you probably got an empty array, which lead to the query to show all categories)

    however, i tried ‘get_the_ID()’ and it seems to work:
    (ps – i never know when to use one or the other – lol)

    <?php 
    
    $bookID = get_the_ID();
    $inCats =  wp_get_post_categories($bookID);
    // alchymyth's suggestion
    // echo $bookID; print_r($inCats);
    listRelated($inCats, $bookID);
    
    ?>

    only tried the echo code and had your function call commented, and it showed the post id and the category ids of the post.

    Thread Starter stevechatterton

    (@stevechatterton)

    Thanks, I’ll have to give that a whirl when I’ve got some time.

    Thanks. This will definitely help me a lot. I will share this to my friends as well.

    [signature moderated Please read the Forum Rules]

    Thread Starter stevechatterton

    (@stevechatterton)

    That does work much better, but because my site’s a bit of a pain in the butt, I’m going to throw another wrench into the works…

    There are 2 category IDs I want to exclude, BUT they are the main parent categories, fiction & non-fiction, for all other books. If I put in a category__not_in statement after the category__in statement I loose everything.

    Is there a simple way to take the array once it’s passed to the function and say drop these 2 values but keep all the others?

    Thread Starter stevechatterton

    (@stevechatterton)

    Answered my own question by visiting this page:
    https://www.scriptygoddess.com/archives/2004/09/21/php-remove-an-element-from-an-array/

    I guess I’m marking as resolved now.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘List categories of a specific post’ is closed to new replies.