Get related posts by custom taxonomy

Published on January 28th, 2014

Photo by Jonathan Simcoe on Unsplash

In this post we’re going to use wp_query and custom taxonomies. Let the fun begin!

What we want is to retrieve posts related to the one we are currently displaying through one (or more) custom taxonomy term by using the following snippet to retrieve the current post type and use it in creative ways. In my case, since I was out of creativity, I just chose to run a simpler query whenever the post_type is not “event”. Check it out.

post_type = get_post_type();

if ($post_type == 'event'){
  $args = array(
    'post_type' => 'post',
    'posts_per_page' => '3',
    'tax_query' => array(
    'relation' => 'AND',
        array(
        'taxonomy' => 'shtags',
        'field' => 'slug',
        'terms' => $myterm,
        'operator' => 'IN'
        )
    )
  $my_posts = new WP_Query($args);
} else {
    $args = array(
    'post_type' => 'post',
    'posts_per_page'  => '1'
    );
  $my_posts = new WP_Query($args);
}

Credits go mainly to Smashing Magazine and hours of Googling in order to search for an easy solution. 

услуги копирайтера

en.topodin.com

продвинуть сайт

roulette spelregels

http://inkisa.org/%D1%84%D0%BE%D1%82%D0%BE-%D0%B8%D0%BD%D1%86%D0%B5%D1%81%D1%82/

seo оптимизация сайта