简体   繁体   中英

How to show related posts by category from different post type

In my website, I have two different post-types. One of them is publication with custom category-type as publication-category and the other is service with custom category-type as service-category . I am publishing some brochures in Publication page those are under different services. What I am trying to do is displaying these brochures in Services page by same category type. It is like, if the brochure is published by Education services, then this brochure should be displayed in Education service page.

I am currently using ACF plugin to do so, but whenever there is new brochure, I have to go to service page and add it there. Today I tried below code but it displays all brochures from different category-types not the same category-type.

Perhaps you guys can help me how I can arrange the code a way that works for my above request.

<?php

$custom_taxterms = wp_get_object_terms( 
    $post->ID, 
    'publication-category', 
    array( 'fields' => 'ids' ) 
);

$args = array(
    'post_type'      => 'publication',
    'post_status'    => 'publish',
    'posts_per_page' => 10,
    'orderby'        => 'rand',
    'order'          => 'ASC',
    'tax_query'      => array( array(
        'taxonomy' => 'publication-category',
        'field'    => 'id',
        'terms'    => $custom_taxterms
    )),
    'post__not_in'   => array( $post->ID ),
);

$related_items = new WP_Query( $args );

if ( $related_items->have_posts() ) :
    echo '<ul>';
    while ( $related_items->have_posts() ) : $related_items->the_post();
    ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php
    endwhile;
    echo '</ul>';
endif;

wp_reset_postdata();
?>

If you're on services page, so why do you use the 'publication-category' in

wp_get_object_terms( 
    $post->ID, 
    'publication-category', 
    array( 'fields' => 'ids' ) 
);

Seems like you have to use

$custom_taxterms = get_the_terms($post->ID, 'service-category');

$terms = [];
foreach ($custom_taxterms as $term) {
  $terms[] = $term->slug
}

$args = array(
    'post_type'      => 'publication',
    'post_status'    => 'publish',
    'posts_per_page' => 10,
    'orderby'        => 'rand',
    'order'          => 'ASC',
    'tax_query'      => array( array(
        'taxonomy' => 'publication-category',
        'field'    => 'slug',
        'terms'    => $terms
    )),
);

And create same slugs for both terms in both categories. From my understanding. It's quite difficult to understand your architecture

I find the solution by changing service-category taxonomy to publication-category as same with other post-type and creating relationship with below code from : https://wordpress.stackexchange.com/questions/139571/display-posts-with-same-taxonomy-term?rq=1

Thanks everyone

  <?php
                                          //get the post's venues
                                      $custom_terms = wp_get_post_terms($post->ID, 'publication-category');

                                      if( $custom_terms ){

                                          // going to hold our tax_query params
                                          $tax_query = array();

                                          // add the relation parameter (not sure if it causes trouble if only 1 term so what the heck)
                                          if( count( $custom_terms > 1 ) )
                                              $tax_query['relation'] = 'OR' ;

                                          // loop through venus and build a tax query
                                          foreach( $custom_terms as $custom_term ) {

                                              $tax_query[] = array(
                                                  'taxonomy' => 'publication-category',
                                                  'field' => 'slug',
                                                  'terms' => $custom_term->slug,
                                              );

                                          }

                                          // put all the WP_Query args together
                                          $args = array( 'post_type' => 'publication',
                                                          'posts_per_page' => 20,
                                                          'tax_query' => $tax_query );

                                          // finally run the query
                                          $loop = new WP_Query($args);

                                          if( $loop->have_posts() ) {

                                              while( $loop->have_posts() ) : $loop->the_post(); ?>

                                              <div class="listing-title"><?php the_title(); ?></div>
                                            <div class="listing-image"><a href="<?php the_permalink() ?>" style="background-image: url('<?php echo get_the_post_thumbnail_url(get_the_ID(), 'full') ?>')"></a>
                                            </div>
                                              <?php

                                              endwhile;

                                          }

                                          wp_reset_query();

                                      }?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM