WordPress.org

Ready to get started?Download WordPress

Forums

WP related posts of custom post type with child parent term relationship (1 post)

  1. wavesdream
    Member
    Posted 1 year ago #

    I have custom post type added on my website and it works fine. There are sevaral customterms are added for custom taxonomy. So, one post is under different terms & sub terms.

    --Term1 --- Sub term1 --- Sub term2 --- Sub term3

    --Term 2 --- Sub term1 --- Sub term2 --- Sub term3

    Say one of my post is under Sub term2 of Term1 and Sub term3 of Term 2. On the single postpage I can show the related posts of the same terms. But I want to show the related postfrom particular one term with child-parent relation. The logic is "If the current post has any term which is the child term of Term1 then display the relatedpost of that child term only.So in the loop it will always check for each term, if it is
    the child of the term1 or not, if yes then it will display the related posts.

    It would be great if anyone provide me the code for this section.
    Here is the code I am currently using in single post page below the post content.

    <?php
    $taxonomyName = "project_categories";
    $termID = 48; // This is the parent term id
    
    $args = array(
    'tax_query' => array(
    array(
    	'taxonomy'  => $taxonomyName,
    	'field'    => 'id',
    	'terms'     => $termID,
    	'operator'  => 'IN'
    )
    ),
    'post_type' => 'projects',
    'post__not_in'          => array( $post->ID ),
    'showposts'             => 3,
    'orderby' 				=> 'rand',
    'ignore_sticky_posts'   => 1
    );
    
    $loop = new wp_query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
    // The items are being displayed here
    
    endwhile;
    wp_reset_query();
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic