Forums

[resolved] List categories of a specific post (11 posts)

  1. stevechatterton
    Member
    Posted 2 years ago #

    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?

  2. PBP_Editor
    Member
    Posted 2 years ago #

  3. stevechatterton
    Member
    Posted 2 years ago #

    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.

  4. MichaelH
    Volunteer
    Posted 2 years ago #

  5. alchymyth
    The Sweeper
    Posted 2 years ago #

    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.

  6. stevechatterton
    Member
    Posted 2 years ago #

    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?

  7. alchymyth
    The Sweeper
    Posted 2 years ago #

    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.

  8. stevechatterton
    Member
    Posted 2 years ago #

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

  9. hezykhaiablane
    Inactive
    Posted 2 years ago #

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

    [signature moderated Please read the Forum Rules]

  10. stevechatterton
    Member
    Posted 2 years ago #

    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?

  11. stevechatterton
    Member
    Posted 2 years ago #

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

    I guess I'm marking as resolved now.

Topic Closed

This topic has been closed to new replies.

About this Topic