I'm using this plugin to add tags for a page: http://wordpress.org/extend/plugins/post-tags-and-categories-for-pages/

And I've added tags support for the custom post:

function codex_custom_init() {
    $args = array(
      'public' => true,
      'has_archive' => true,
      'label'  => 'Doing',
      'taxonomies' => array('post_tag'),
      'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt')
    register_post_type( 'doing', $args );
add_action( 'init', 'codex_custom_init' );

In every page with an specific tag I want to display related custom posts that have the same tag as the page, is this possible to do? How can I get to load on the page the tag related custom posts?

I've tried with this code but is not showing any related post:

      $tags = wp_get_post_tags($post->ID);
      if ($tags) {
          echo 'Related Posts';
          $first_tag = $tags[0]->term_id;
              'tag__in' => array($first_tag),
              'post__not_in' => array($post->ID),
          $my_query = new WP_Query($args);
          if( $my_query->have_posts() ) {
              while ($my_query->have_posts()) : $my_query->the_post(); ?>
              <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>

  • Where did you put that related posts code ?
    – Sumit
    Commented May 30, 2016 at 11:17
  • @Sumit I've put that code in the page template, not in the custom posts template.
    – wpuser
    Commented May 30, 2016 at 14:54

2 Answers 2


You need to specify a post type in your WP_Query arguments when you need to query any other post type accept the build in post type post.

By default, post_type is set to post, so when no specific post type is set manually by the user, WP_Query will query posts from the post type post

Additionally, caller_get_posts have been deprecated for a very very long time now. If you had debug turned on, you would have recieved a deprecation notice about this. The correct arguments to use is ignore_sticky_posts.

I would also not use the $post global as it is unreliable, for reliability, rather use $GLOBALS['wp_the_query']->get_queried_object(). You can read up on this subject in my answer here

get_the_tags() is also faster than wp_get_post_tags() as the latter requires an extra db call.

One last note, wp_reset_query() is used with query_posts, the correct function to use with WP_Query is wp_reset_postdata()

In essence, you can try the following

$post_id = $GLOBALS['wp_the_query']->get_queried_object_id();
$tags = get_the_tags( $post_id );
if (    $tags
     && !is_wp_error( $tags ) 
) {
    $args = [
        'post__not_in' => [$post_id],
        'post_type'    => 'doing',
        'tag__in'      => [$tags[0]->term_id],
        // Rest of your args
    $my_query = new WP_Query( $args );

    // Your loop

  • Pieter, I'm using this code on my page template. The page has the same tag as a doing custom post. But the output when I put echo the_title(); in the // Your loop part is another type of custom post which doesn't even has the 'taxonomies' => array('post_tag') support :/
    – wpuser
    Commented May 30, 2016 at 14:44
  • @wpuser This is the crystal clear answer. Not sure why you are having troubles! When you echo the_title(); did you setup the postdata using the_post() ?
    – Sumit
    Commented May 30, 2016 at 15:06
  • It works perfect! Thanks Pieter for the great explanation, again :) I'll award the bounty as soon as its possible to do it.
    – wpuser
    Commented May 30, 2016 at 15:17

My first port of call is to call global $post; - as that sets you up to use $post->ID ... if you are using it then the next step is to display each part separately e.g.


echo $first_tag;

see what's displayed in each part, this will help diagnose where the issue is, as without seeing your code working, we can't say why it's not working.

  • The output I get is array() :/ I put it like you suggested: <?php global $post; $tags = wp_get_post_tags($post->ID); print_r($tags); echo $first_tag; ?>
    – wpuser
    Commented May 26, 2016 at 19:53
  • if you get array then that means it's empty, so you need to work out why it's empty before you can move onto the next step. Commented May 26, 2016 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.