0

I am trying to Get List of all custom post type (Album) for the current tag (post_tag) page (tag.php).

Lemme explain further:

  1. E.g I used Drake has a post_tag taxonomy while posting Albums (album post type)
  2. Now on Drake archive page (tag.php) which displays all posts, albums and songs by drake
  3. I want to display the List of Albums by Drake which is the current tag on a different section of the page (tag.php).
  4. E.g I have three Tabs [All Posts by Drakes], [Drake Albums], [Drake Songs], I want to get only drake albums list.

Note: I want this to be the case for every tag.

I tried the following code in my tag.php but it lists all post from different post types for the current tag:

<?php
$tag = get_query_var('tag');

$args = array(
    'post_type' => 'album',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'paged' => $paged,
    'tag' => $tag,
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) :

    if ( $my_query->have_posts() ) while ( $my_query->have_posts() ) :  $my_query->the_post(); 
?>
<a href="<?php echo get_permalink(get_the_ID()); ?>"><?php the_title(); ?></a>
<?php endwhile; // end of the loop.
    endif;
wp_reset_query();
?>

Please I need help with this to make it display a list of all albums only for the current tag.

NOTE: I have the following in my function.php which adds all post types to fronts like tag, category and home:

function add_custom_types_to_tax( $query ) {
if( is_category() || is_home() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
// Get all your post types
$post_types = array( 'post', 'album', 'music' );
$query->set( 'post_type', $post_types );
return $query;
}
}
add_filter( 'pre_get_posts', 'add_custom_types_to_tax' );

Your Answer

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

Browse other questions tagged or ask your own question.