Luiz Stelzer
Luiz Stelzer

Reputation: 79

Related posts category in wordpress (taxonomy)

How to do with this code only show the "related posts by category"? This code is inside the "single.php" I did not stackoverflow but could not find a solution. Can anyone help me?

<?php  
     $postsPerPageq = 5;

               $allargw = array(
              'post_type' => 'audioplayer',
              'posts_per_page' => $postsPerPageq,
               'orderby' => 'title',
              'order' => 'DESC',
             'tax_query' => array(
        array(
            'taxonomy' => 'audiotop',
            'field' => 'slug',
            'terms' => 'top-audio'
        )
     )
             );
               $topaudio = new WP_Query($allargw);
    while ( $topaudio->have_posts() ) : $topaudio->the_post();  


    ?>

<h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3>


<?php endwhile;   wp_reset_postdata();?>

Upvotes: 0

Views: 826

Answers (3)

Prateek Verma
Prateek Verma

Reputation: 889

if you want to retrieve posts by simple posts category then check this code:

<?php  
     $category = get_the_category($post->ID);
     $cat_id = $category[0]->term_id;
     $postsPerPageq = 5;
     $allargw = array(
              'post_type' => 'audioplayer',
              'posts_per_page' => $postsPerPageq,
               'orderby' => 'title',
              'order' => 'DESC',
              'cat' => $cat_id

            );
    $topaudio = new WP_Query($allargw);
    while ( $topaudio->have_posts() ) : $topaudio->the_post();   ?>
        <h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3>
    <?php endwhile;   wp_reset_postdata();?>

else if you want to retrieve posts by custom taxonomy then check the code below:

<?php  
     $term_list = wp_get_post_terms($post->ID, 'audiotop', array("fields" => "all"));
     $term_id = $term_list[0]->term_id ;
     $postsPerPageq = 5;
     $allargw = array(
                'post_type' => 'audioplayer',
                'posts_per_page' => $postsPerPageq,
                'orderby' => 'title',
                'order' => 'DESC',
                'tax_query' => array(
            array(
                'taxonomy' => 'audiotop',
                'field' => 'id',
                'terms' => $term_id
            )
        )
    );
    $topaudio = new WP_Query($allargw);
    while ( $topaudio->have_posts() ) : $topaudio->the_post();  ?>
        <h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3>
    <?php endwhile;   wp_reset_postdata();?>

Hope, this will help you.

Upvotes: 0

Preethi
Preethi

Reputation: 101

Add category name in your allargw array value..

<?php  
  $postsPerPageq = 5;

           $allargw = array(
          'post_type' => 'audioplayer',
          'posts_per_page' => $postsPerPageq,
           'category_name'=>'Your category name',
           'orderby' => 'title',
          'order' => 'DESC',
         'tax_query' => array(
    array(
        'taxonomy' => 'audiotop',
        'field' => 'slug',
        'terms' => 'top-audio'
    )
 )
         );
           $topaudio = new WP_Query($allargw);
while ( $topaudio->have_posts() ) : $topaudio->the_post();  


?>

<h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3>

Hope this helps to you!!

Updated answer:

If you want add category automatically, use like this..

wp_set_object_terms( 'your_post_id', 'your_category_name', 'your_taxonomy_name');

You can try this..

Upvotes: 0

Nahid Hasan
Nahid Hasan

Reputation: 471

First, you need to grab current post terms using this function

get_the_terms ( get_the_id(), 'your_custom_taxonomy' );

then you grab the terms slug for tax_query in your WP_Query.

$postsPerPageq = 5;
$terms_slugs = array();

//get the current post terms slug
$terms = get_the_terms( get_the_id(), 'audiotop' ); 
foreach ( $terms as $term) {
   $terms_slugs[] = $term->slug;
}

//WP_Query args
$allargw = array(
  'post_type' => 'audioplayer',
  'posts_per_page' => $postsPerPageq,
  'orderby'   => 'title',
  'order'     => 'DESC',
  'tax_query' => array(
     array(
       'taxonomy' => 'audiotop',
       'field' => 'slug',
       'terms' => $terms_slugs,
     )
   )
);

i hope it your solve your problem.

Upvotes: 1

Related Questions