• Home
  • About Me
  • Disclaimer
  • Privacy Policy
  • Portfolio Site
  • WordPress Development Service
  • WP Hosting

Banna360

WordPress Codes | Mobile App Reviews | Web, Android, iOS, Technology and More

Web, Android, iOS, Technology and More…

Mobile App Reviews | WordPress Codes

  • Home
  • Blog
  • iOS App Reviews
  • WordPress
  • Web Design
  • Web Development
  • PHP
  • General
  • Contact
You are here: Home / Web Development / Display Related Post or Product based on Taxonomy in WordPress

Display Related Post or Product based on Taxonomy in WordPress

Related post display in for custom post type is very easy. But if you have a custom taxonomy it is little tricky. You can use the following code to display related post from a post type. This code very useful to display related product while you are using custom taxonomy.

<?php

            // get the custom post type's taxonomy terms

            $custom_taxterms = wp_get_object_terms( $post->ID, 'product_category', array('fields' => 'ids') );
            // arguments
            $args = array(
            'post_type' => 'product',
            'post_status' => 'publish',
            'posts_per_page' => 4, // you may edit this number
            'orderby' => 'rand',
            'tax_query' => array(
                array(
                    'taxonomy' => 'product_category',
                    'field' => 'id',
                    'terms' => $custom_taxterms
                )
            ),
            'post__not_in' => array ($post->ID),
            );
            $related_items = new WP_Query( $args );
            // loop over query
            if ($related_items->have_posts()) :

            while ( $related_items->have_posts() ) : $related_items->the_post();
            ?>
            <div class="related_item">
              <div class="Related_image">
                <a href="<?php the_permalink() ?>"><?php echo get_the_post_thumbnail( ); ?></a>
              </div>
                <div class="Related_title">
                  <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><h2>
                   
                </div>
              </div>
            <?php
            endwhile;

            endif;
            // Reset Post Data
            wp_reset_postdata();
            ?>

In This post my Taxony name is product_category and post type name is product

Don’t forget to change this two variable as per your name.  My result of code.

Filed Under: Web Development, Wordpress

Facebook Fans

Banna360

Recents Updates

Top backup plugins for WordPress

Contact Form 7 Boilerplate for Bootstrap CSS theme

GST For Freelancers and Service Sector

Display Related Post or Product based on Taxonomy in WordPress

Categories

  • Android App
  • Featured Post
  • General
  • iOS App Reviews
  • PHP
  • Web Design
  • Web Development
  • Wordpress

Android Apps

Friendly App review – The best alternative app for Facebook lite and messenger

If you are a person who likes to keep your personal life away from the work, Then Friendly App is for you. Multiple Facebook accounts on a single app, sounds great right? This app has a lot more to offer to make your Facebook experience even better. The Friendly For Facebook app is built by getting […]

My Jio App Review – The best way to track down your Jio Data usage

My Jio is an exclusive app from Reliance Jio digital service for its jio users where one can do tasks like keeping track on their usage to managing and recharging their Jio account. My Jio app had gone through many updates after its release, in the latest update for android the version 3.2.42 has nothing […]

About Me

Banna a professional Web developer with a specialization in WordPress web design, having worked on more than 200 individual WordPress projects and sites over the course of my career.As a specialist in WordPress development, I can help with all aspects of taking your WordPress project from start to finish.

READ MORE ABOUT ME

Recent Posts

  • UpdraftPlus – A Complete Study
  • Top backup plugins for WordPress
  • Contact Form 7 Boilerplate for Bootstrap CSS theme
  • GST For Freelancers and Service Sector
  • Display Related Post or Product based on Taxonomy in WordPress

Tags

autoupdate wordpress best translator app bhim app contact form 7 Friendly App review Gst freelancers LinkedIn redesign metabox my jio app MYSQL Productivity tips shortcode api upi app for ios whatsapp bug whatsapp uae why telegram wordpress.org wordpress hosting wordpress plugins wordpress tutorial zip using php

Copyright © 2023 · Magazine Pro Theme on Genesis Framework · WordPress · Log in