Skip to content

Instantly share code, notes, and snippets.

@ihorduchenko
Created December 1, 2017 15:03
Show Gist options
  • Save ihorduchenko/0f8227ec6073dd378fa54d221672e822 to your computer and use it in GitHub Desktop.
Save ihorduchenko/0f8227ec6073dd378fa54d221672e822 to your computer and use it in GitHub Desktop.
Get list of custom taxonomies of custom post type and list of the posts under them
//Downloads custom post type
function add_downloads_posts() {
register_post_type(
'download',
array(
'labels' => array(
'name' => 'Downloads',
'singular_name' => 'Downloads item',
'add_new' => 'Add new',
'add_new_item' => 'Add new',
'edit' => 'Edit',
'edit_item' => 'Edit',
'new_item' => 'Add new',
'view' => 'Edit',
'view_item' => 'Edit',
'search_items' => 'Search',
'not_found' => 'No downloads',
'not_found_in_trash' => 'Trash is empty',
),
'public' => true,
'hierarchical' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-download',
'taxonomies' => array( 'type' ),
'supports' => array(
'title',
),
'can_export' => true,
)
);
}
add_action( 'init', 'add_downloads_posts' );
//Add "type" taxonomy to downloads post type
function create_types_hierarchical_taxonomy() {
$labels = array(
'name' => 'Types',
'singular_name' => 'Type',
'search_items' => 'Search Types' ,
'all_items' => 'All Types' ,
'parent_item' => 'Parent Type' ,
'parent_item_colon' => 'Parent Type:' ,
'edit_item' => 'Edit Type' ,
'update_item' => 'Update Type' ,
'add_new_item' => 'Add New Type' ,
'new_item_name' => 'New Type Name' ,
'menu_name' => 'Types' ,
);
register_taxonomy('types',array('download'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'type' ),
));
}
add_action( 'init', 'create_types_hierarchical_taxonomy', 0 );
<?php $terms = get_terms( 'types' ); ?>
<?php
$allcats = get_terms( 'types' );
foreach ( $allcats as $cat ) :
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'types',
'field' => 'term_id',
'terms' => array( $cat->term_id )
)
)
);
$customInCatQuery = new WP_Query( $args );
if ( $customInCatQuery->have_posts() ) : ?>
<div class="column-22 downloads-column w-col w-col-4 w-col-medium-4">
<h1 class="downloads-column_title"><?php echo $cat->name; ?></h1>
<?php while ( $customInCatQuery->have_posts() ) : $customInCatQuery->the_post(); ?>
<a class="downloads-column_link" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</div>
<?php endwhile; endif;
wp_reset_query(); endforeach; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment