0

I am trying to create custom post & post tags with following code

function cptportfolioPost_init() {
  register_post_type( 'portfolio',
    array(
      'labels' => array(
        'name' => __( 'Portfolio' ),
        'singular_name' => __( 'Portfolio' )
      ),
      'public' => true,
      'has_archive' => true,
      'menu_icon' => 'dashicons-calendar-alt',
      'taxonomies' => array('post_tag'),
      'supports' => array(
            'title',
            'editor',
            'excerpt',
            'revisions',
            'thumbnail',
            'author',
            'page-attributes',
        )
    )
  );
}
add_action( 'init', 'cptportfolioPost_init' );

get tags on the post page with this code

<?php $tags = get_tags();
               $html = '<div class="post-tags-wrap">';
               foreach ( $tags as $tag ) {
                  $tag_link = get_tag_link( $tag->term_id );

                  $html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
                  $html .= "{$tag->name}</a>";
               }
               $html .= '</div>';
               echo $html;?>

in dashboard my custom post has a tags option, in frontend on post page selected tags also displayed but when I click tags link, tags page doesn't display custom post related to that tag only display WordPress default post related to that tag So how can I get my custom post on the tags page

0

WordPress tags only display default posts on their page. In order to display custom post type in tags page, you will have to add your post type in default query of the tag.

Add below code in your functions.php and add custom post types in $post_types array which you want to display on tags page.

function add_custom_types_to_tax( $query ) {
    if( is_tag() && empty( $query->query_vars[‘suppress_filters’] ) ) {

        // Get all Custom Post types
        //$post_types = get_post_types();

        // Get Specific Custom Post types
        $post_types = array( ‘post’, ‘your_custom_type’ );

        $query->set( ‘post_type’, $post_types );

        return $query;
    }
}
add_filter( ‘pre_get_posts’, ‘add_custom_types_to_tax’ );

Hope this helps..!!

0

In functions.php I added:

function tag_filter($query) {
  if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_tag) {
      $query->set('post_type', array( 'custom_post_type', ));
    }
  }
}
add_action('pre_get_posts','tag_filter');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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