Changing Related Post Selection From Tags to Categories

Hey Everyone.

I am hoping this is the right place to ask this question.

I am using a new WordPress theme and it has native related post coding in it. The problem is the code uses Post Tags as the selection value instead of Post Category. I was wondering if anyone could help me with this strip of code. I want to change it so the theme gets Posts in the same Category, instead of Posts with the same Tags.

[php]* Display related posts.
*

  • @return void
    */
    function stag_related_posts() {
    global $post;
    if( stag_rcp_user_has_no_access() )
    return;
    if( ! stag_theme_mod( ‘post_settings’, ‘show_related_posts’ ) )
    return;
    $tags = wp_get_post_tags( $post->ID );
    if ( count( $tags ) ) {
    $tags_ids = array();
    foreach($tags as $individual_tag) {
    $tag_ids[] = $individual_tag->term_id;
    }
    $query = new WP_Query( array(
    ‘tag__in’ => $tags_ids,
    ‘post__not_in’ => array( $post->ID ),
    ‘posts_per_page’ => absint( stag_theme_mod( ‘post_settings’, ‘related_posts_count’ ) ),
    ‘caller_get_posts’ => 1
    ) );
    // Add filter to attach ‘post-grid’ class
    add_filter( ‘stag_showing_related_posts’, ‘__return_true’ );
    if( $query->have_posts() ) :
    echo ‘’;
    while( $query->have_posts() ) : $query->the_post();
    get_template_part( ‘content’, get_post_format() );
    endwhile;
    echo ‘’;
    endif;
    }
    wp_reset_query();
    remove_all_filters( ‘stag_showing_related_posts’ );
    }[/php]

I’ve tried simply replaces the words Tags with Categories, but it doesn’t seem to do much.

Any help would be greatly, greatly, appreciated.

My suggestion is to check the function

wp_get_post_tags( $post->ID );

Create your own wp_get_post_categories( $post->ID ); and then process it down with the categories yourself

Sponsor our Newsletter | Privacy Policy | Terms of Service