• Hi,
    I’m trying to display related posts based on custom taxonomy.
    I created 4 custom taxonomies and I’m using them instead of regular tags. I want to display related articles based on 1 or more taxonomy but I can’t find any answers on how to do that.
    This is the code that was displaying related articles based on tags. I can’t make it work for my custom tags.

    <?php
                        // related entries based on tags
                        $backup = $post; // backup the current object
                        $tags = wp_get_post_tags( $post->ID );
                        $tagIDs = array();
                        if( $tags ) {
                            $tagcount = count( $tags );
                            for( $i = 0; $i < $tagcount; $i++ ) {
                                $tagIDs[ $i ] = $tags[ $i ]->term_id;
                            }
                            $args = array(
                                'tag__in' => $tagIDs,
                                'post__not_in' => array( $post->ID ),
                                'showposts' => 5,
                                'caller_get_posts' => 1
                            );
                            $my_query = new WP_Query( $args );
                            if( $my_query->have_posts() ) {
    						?>

    Any idea?
    Thanks.
    Chris

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter asthyanax

    (@asthyanax)

    Actually he shows how to display for a specific taxonomy. But like for tags I can’t see in his article where you can simply put like single_tax_title() and it shows the title of taxonomy selected. I’m a bit lost on taxonomy it seems hard and complicated. I could easily create custom taxonomy and tag clouds for them but now I can’t really display posts related to a taxonomy or display the title of the taxonomy. Any good link on that?
    Thanks.

    Don’t know why you couldn’t use these:

    Function_Reference/get_terms
    Function_Reference/get_term_by

    Thread Starter asthyanax

    (@asthyanax)

    I just don’t understand where to use get_terms for example to query the posts with the same taxonomy.
    I tried to see what get_terms does in my single.php file and it does nothing or it echoes ‘Array’
    $myterms = get_terms(‘course’, ‘orderby=count&hide_empty=0’);
    echo $myterms;

    There’s nothing like wp_get_post_taxonomy( $post->ID ); by chance?
    I looked for days and I can’t find anything. Even plugin for related posts don’t work with custom taxonomy.
    I keep looking.
    Thanks.

    Are you using a plugin for your custom taxonomy? Is so which one?

    For tax archive titles on my archive page I have been using is_tax() and then in place of the non-existent single_tax_title() use get_query_var(‘name_of_your_tax’). Caveats: this requires query_var => true when registering your taxonomy and a permalink structure that supports query_vars.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Related posts based on taxonomy’ is closed to new replies.