Skip to content

Instantly share code, notes, and snippets.

@rianrietveld
Created November 22, 2012 06:43
Show Gist options
  • Save rianrietveld/4129704 to your computer and use it in GitHub Desktop.
Save rianrietveld/4129704 to your computer and use it in GitHub Desktop.
WordPress costom post type with it's own taxonomy. Also adds an archive option to show the posts ordered by taxonomy name
<?php
/**
* This code adds a costom post type with it's own taxonomy to WordPress
* And adds an archive option to show the posts ordered by taxonomy name
* Rian Rietveld - rrwd.nl
* November 22, 2012
* Add this to the functions.php in your WordPress child theme
* Here, as an example, the custom post type is book and the taxonony is genre
*/
// define and register the custom post type
function register_prefix_name_cpt() {
$labels = array(
'name' => _x('Books', 'post type general name', 'your_text_domain'),
'singular_name' => _x('Book', 'post type singular name', 'your_text_domain'),
'add_new' => _x('Add New', 'book', 'your_text_domain'),
'add_new_item' => __('Add New Book', 'your_text_domain'),
'edit_item' => __('Edit Book', 'your_text_domain'),
'new_item' => __('New Book', 'your_text_domain'),
'all_items' => __('All Books', 'your_text_domain'),
'view_item' => __('View Book', 'your_text_domain'),
'search_items' => __('Search Books', 'your_text_domain'),
'not_found' => __('No books found', 'your_text_domain'),
'not_found_in_trash' => __('No books found in Trash', 'your_text_domain'),
'parent_item_colon' => __('Parent item: ', 'your_text_domain'),
'menu_name' => __('Books', 'your_text_domain')
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'description' => __( 'Description here','your_text_domain'),
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
'taxonomies' => array( 'prefix_name_taxonomy' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 21,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => array( 'slug' => __( 'book', 'URL slug', 'your_text_domain' ) ),
'capability_type' => 'page'
);
register_post_type( 'prefix_name_cpt', $args );
}
add_action( 'init', 'register_prefix_name_cpt' );
// add the custom post type to the query results
function prefix_get_posts( $query ) {
if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) && !is_admin() )
$query->set( 'post_type', array( 'post', 'page', 'prefix_name_cpt', 'nav_menu_item','prefix_name_taxonomy' ) );
return $query;
}
add_action( 'pre_get_posts', 'prefix_get_posts' );
// define and register the taxonony
function prefix_create_prefix_name_taxonomy() {
$labels = array(
'name' => _x( 'Genres', 'taxonomy general name' ),
'singular_name' => _x( 'Genre', 'taxonomy singular name' ),
'search_items' => __( 'Search Genres' ),
'all_items' => __( 'All Genres' ),
'parent_item' => __( 'Parent Genre' ),
'parent_item_colon' => __( 'Parent Genre:' ),
'edit_item' => __( 'Edit Genre' ),
'update_item' => __( 'Update Genre' ),
'add_new_item' => __( 'Add New Genre' ),
'new_item_name' => __( 'New Genre Name' ),
'menu_name' => __( 'Genre' ),
);
register_taxonomy( 'prefix_name_taxonomy', array( 'prefix_name_cpt' ), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'name_taxonomy' ),
) );
}
add_action( 'init', 'prefix_create_prefix_name_taxonomy', 0 );
/**
* Get the custom post type, ordered alphabetically by taxonomy name
* The posts are ordered by date
* The taxonomies are placed inside a div, with an H2 as heading
* This function can (for example) be added in a template file archive-prefix_name_cpt.php of your child theme
*
*/
function prefix_show_ctp_by_taxonomy() {
// thanks to nickam at http://wordpress.org/support/topic/list-posts-by-taxonomy-tag
$post_type = 'prefix_name_cpt';
$tax = 'prefix_name_taxonomy';
$tax_terms = get_terms( $tax, 'hide_empty=0' );
$nr_div = 1; // counter for the displayed div's, to add extra style at the third block
// list for each taxonomy, list the posts
if ( $tax_terms ) {
foreach ( $tax_terms as $tax_term ) {
$args = array(
'post_type' => $post_type,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'ordery' => 'date'
);
$my_query = null;
$my_query = new WP_Query ($args );
if( $my_query->have_posts() ) {
if ( fmod($nr_div, 3) == 0 ) { // add an extra style class to the third div for changing margins for example
echo '<div class="prefix-cpt-tax-div last">'."\n";
} else {
echo '<div class="prefix-cpt-tax-div">'."\n";
}
echo ' <h2>' . $tax_term->name . "</h2>\n";
echo " <ul>\n";
while ( $my_query->have_posts() ) : $my_query->the_post();
?> <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li><?php
endwhile;
echo " </ul> \n";
echo "</div><!-- end .prefix-cpt-tax-div --> \n";
$nr_div++;
}
wp_reset_query();
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment