Displaying Custom Post Type Using Default Archive Template
First, you can simply go to Appearance » Menus and add a custom link to your menu.
This custom link is the link to your custom post type.
If you are using SEO friendly permalinks then your CPT’s URL will most likely be something like this:
If you are not using SEO friendly permalinks, then your custom post type URL will be something like this:
it will display your custom post type archive page using the
archive.php template file in your theme.
CREATE NEW TEMPLATE TO DISPLAY:::
Using Custom Templates for CPT Archives and Single Entries
If you don’t like the appearance of the archive page for your custom post type, then you can use dedicated template for custom post type archive.
To do that all you need to do is create a new file in your theme directory and name it archive-movies.php. Replace movies with the name of your custom post type.
For getting started, you can copy the contents of your theme’s archive.php file into archive-movies.php template and then start modifying it to meet your needs.
Now whenever the archive page for your custom post type is accessed, this template will be used to display it.
Similarly, you can also create a custom template for your post type’s single entry display. To do that you need to create single-movies.php in your theme directory. Don’t forget to replace movies with the name of your custom post type.
You can get started by copying the contents of your theme’s single.php template into single-movies.php template and then start modifying it to meet your needs.
DISPLAY AMONG REGULAR POSTS:::::
then you can do so by adding this code into your theme’s functions.php file or a site-specific plugin:
add_action( ‘pre_get_posts’, ‘add_my_post_types_to_query’ );
function add_my_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( ‘post_type’, array( ‘post’, ‘movies’ ) );
return $query;
}
Don’t forget to replace movies with your custom post type.
QUERY IN LOOP:::::::
Querying Custom Post Types
If you are familiar with the coding and would like to run loop queries in your templates, then here is how to do that (Related: What is a Loop?).
By querying the database, you can retrieve items from a custom post type.
<?php
$args = array( ‘post_type’ => ‘movies’, ‘posts_per_page’ => 10 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class=”entry-content”>
<?php the_content(); ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( ‘Sorry, no posts matched your criteria.’ ); ?></p>
<?php endif; ?>
In this code, first, we have defined the post type and posts per page in the arguments for our new WP_Query class.
After that, we ran our query, retrieved the posts and displayed them inside the loop.
USER WIDGET TO DISPLAY::::::
Displaying Custom Post Types in Widgets
You will notice that there is a default widget in WordPress to display recent posts, but it does not allow you to choose a custom post type.
What if you wanted to display the latest entries from your newly created post type in a widget? There is an easy way to do this.
First thing you need to do is install and activate the Ultimate Posts Widget plugin. Upon activation, simply go to Appearance » Widgets and drag and drop the Ultimate Posts widget to a sidebar.
Ultimate posts widget
This powerful widget will allow you to show recent posts from any post types. You can also display post excerpts with a read more link or even show a featured image next to post title.
Configure the widget by selecting the options you want and by selecting your custom post type. After that save your changes and see the widget in action on your website.
RSS / MORE WAYS TO DISPLAY:::
There is so much more you can do with your custom post types. You can learn to add your custom post types in main
www.wpbeginner.com/wp-tutorials/how-to-add-custom-post-types-to-your-main-wordpress-rss-feed/
RSS feed or create a
www.wpbeginner.com/wp-tutorials/how-to-make-a-separate-rss-feed-for-each-custom-post-type-in-wordpress/
separate feed for each custom post type.
For more hacks, see our list of the most useful WordPress custom post types tutorials.
If you’re looking for a no-code solution to customize your custom post type archive pages, then we recommend taking a look at a WordPress page builder plugin
www.wpbeginner.com/beginners-guide/best-drag-and-drop-page-builders-for-wordpress/
like Beaver Builder or Divi because they both can help you do that.
We hope this article helped you learn how to create custom post types in WordPress. You may also want to see our guide on how to increase your website traffic with practical tips.
Basic Custom Post Type – Issues / Solutions
www.wpbeginner.com/wp-tutorials/12-most-useful-wordpress-custom-post-types-tutorials/
Dynamic Menu Items – Custom Post Types
www.outsource-wordpress.com/custom-post-type-posts-in-menu-dynamically/
www.outsource-wordpress.com/custom-post-type-posts-in-menu-dynamically/
Custom Post Type Posts in Menu Dynamically
SIMPLE SUB MENU
[php] <!–mep-nl–><!–mep-nl–>function nacin_register_slideshows_post_type() {
<!–mep-nl–> <!–mep-tab–>register_post_type( ‘slideshow’, array(
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>’labels’ => array(
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–> <!–mep-tab–>’name’ => ‘Slideshows’,
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–> <!–mep-tab–>’singular_name’ => ‘Slideshow’,
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>),
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>’public’ => true,
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>’show_ui’ => true,
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>’show_in_menu’ => ‘edit.php’,
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>’supports’ => array( ‘title’ ,’thumbnail’, ‘editor’ ),
<!–mep-nl–> <!–mep-tab–>) );
<!–mep-nl–>}
<!–mep-nl–>add_action( ‘init’, ‘nacin_register_slideshows_post_type’ );
<!–mep-nl–>
<!–mep-nl–>
<!–mep-nl–>
<!–mep-nl–>
<!–mep-nl–>
<!–mep-nl–> /*————————————*\
<!–mep-nl–> <!–mep-tab–>ADD SIMPLE SUB MENU FOR CUSTOM FIELD
<!–mep-nl–> \*————————————*/
<!–mep-nl–>
<!–mep-nl–>
<!–mep-nl–>
<!–mep-nl–>function nacin_register_slideshows_post_type() {
<!–mep-nl–> <!–mep-tab–>register_post_type( ‘slideshow’, array(
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>’labels’ => array(
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–> <!–mep-tab–>’name’ => ‘Slideshows’,
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–> <!–mep-tab–>’singular_name’ => ‘Slideshow’,
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>),
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>’public’ => true,
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>’show_ui’ => true,
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>’show_in_menu’ => ‘edit.php’,
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>’supports’ => array( ‘title’ ,’thumbnail’, ‘editor’ ),
<!–mep-nl–> <!–mep-tab–>) );
<!–mep-nl–>}
<!–mep-nl–>add_action( ‘init’, ‘nacin_register_slideshows_post_type’ );
<!–mep-nl–>
<!–mep-nl–>
<!–mep-nl–>
<!–mep-nl–> /*————————————*\
<!–mep-nl–> <!–mep-tab–>ANOTHER SUB MENU FOR CUSTOM POSTS
<!–mep-nl–> \*————————————*/
<!–mep-nl–>
<!–mep-nl–>
<!–mep-nl–>add_filter( ‘wp_get_nav_menu_items’, ‘cpt_locations_filter’, 10, 3 );
<!–mep-nl–>
<!–mep-nl–>function cpt_locations_filter( $items, $menu, $args ) {
<!–mep-nl–> $child_items = array();
<!–mep-nl–> $menu_order = count($items);
<!–mep-nl–> $parent_item_id = 0;
<!–mep-nl–>
<!–mep-nl–> foreach ( $items as $item ) {
<!–mep-nl–> <!–mep-tab–>if ( in_array(‘locations-menu’, $item->classes) ){ //add this class to your menu item
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>$parent_item_id = $item->ID;
<!–mep-nl–> <!–mep-tab–>}
<!–mep-nl–> }
<!–mep-nl–>
<!–mep-nl–> if($parent_item_id > 0){
<!–mep-nl–>
<!–mep-nl–> <!–mep-tab–> foreach ( get_posts( ‘post_type=cpt-post-type-here&numberposts=-1’ ) as $post ) {
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>$post->menu_item_parent = $parent_item_id;
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>$post->post_type = ‘nav_menu_item’;
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>$post->object = ‘custom’;
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>$post->type = ‘custom’;
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>$post->menu_order = ++$menu_order;
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>$post->title = $post->post_title;
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>$post->url = get_permalink( $post->ID );
<!–mep-nl–> <!–mep-tab–> <!–mep-tab–>array_push($child_items, $post);
<!–mep-nl–> <!–mep-tab–> }
<!–mep-nl–>
<!–mep-nl–> }
<!–mep-nl–>
<!–mep-nl–> return array_merge( $items, $child_items );
<!–mep-nl–>}
<!–mep-nl–>
<!–mep-nl–>
<!–mep-nl–>
<!–mep-nl–>[/php]
You may want to add your Custom Post Type posts dynamically in the menu as dropdown and here is the solution.
Step 1:
code.tutsplus.com/tutorials/understanding-the-walker-class–wp-25401
Paste the following code in your theme’s ‘functions.php’. This will utilize the WordPress menu Walker
class – end_el
function.
<!–mep-nl–>class Walker_Dynamic_Submenu extends Walker_Nav_Menu {
<!–mep-nl–>function end_el(&$output, $item, $depth=0, $args=array()) {
<!–mep-nl–>if( 100 == $item->ID ){
<!–mep-nl–>global $post;
<!–mep-nl–>$cptnames = get_posts(array(‘post_type’=>’cptname’,’posts_per_page’=>-1, ‘post_parent’ => 0));
<!–mep-nl–>if(!empty($cptnames)) {
<!–mep-nl–>$output .= ‘<ul class="sub-menu">’;
<!–mep-nl–>foreach($cptnames as $post): setup_postdata($post);
<!–mep-nl–>$output .= ‘<li><a href="’.get_permalink().’">’.get_the_title().’</a>’;
<!–mep-nl–>$output .= get_child_posts(get_the_ID());
<!–mep-nl–>$output .= ‘</li>’;
<!–mep-nl–>endforeach; wp_reset_postdata();
<!–mep-nl–>$output .= ‘</ul>’;
<!–mep-nl–>}
<!–mep-nl–>}
<!–mep-nl–>$output .= "</li>\n";
<!–mep-nl–>}
<!–mep-nl–>}
<!–mep-nl–>[/php]
Replace cptname with your Custom Post Type name above.
Step 2:
Again paste the following code in your theme’s ‘functions.php’ if you have parent-child hierarchy for your Custom Post Type to display the children. You can skip this step if you don’t have hierarchy.
<!–mep-nl–>function get_child_posts($parent = 0) {
<!–mep-nl–>global $post;
<!–mep-nl–>$cptnameschild = get_posts(array(‘post_type’=>’cptname’,’posts_per_page’=>-1, ‘post_parent’ => $parent));
<!–mep-nl–>if(!empty($cptnameschild)) {
<!–mep-nl–>$outputchild .= ‘<ul class="sub-menu">’;
<!–mep-nl–>foreach($cptnameschild as $post): setup_postdata($post);
<!–mep-nl–>$outputchild .= ‘<li><a href="’.get_permalink().’">’.get_the_title().’</a></li>’;
<!–mep-nl–>$outputchild .= get_child_posts(get_the_ID());
<!–mep-nl–>endforeach; wp_reset_postdata();
<!–mep-nl–>$outputchild .= ‘</ul>’;
<!–mep-nl–>}
<!–mep-nl–>return $outputchild;
<!–mep-nl–>}
<!–mep-nl–>[/php]
Again, replace cptname with your Custom Post Type name above.
Step 3:
Now we are in the final part – we need to append our Walker function to the WordPress menu items. Add this ,
<!–mep-nl–>[/php]
RSS Feeds for Custom Post Types
WordPress comes with a built-in RSS generator for all content types, taxonomies, authors, and date based archives. It uses a proper URL structure that queries the database and generates the RSS feed you want to see.
For example, to see the RSS feed of your custom post type ‘movies’ you will add this URL:
www.example.com/feed/?post_type=movies
For more details, see how to make
www.wpbeginner.com/wp-tutorials/how-to-make-a-separate-rss-feed-for-each-custom-post-type-in-wordpress/
separate RSS feed for custom post types in WordPress.
You can easily include your custom post types into your main RSS feed by adding the following code to your theme’s functions.php file or a site-specific WordPress plugin.
[php] <!–mep-nl–>&lt;!–mep-nl–&gt;function myfeed_request($qv) {<!–mep-nl–>&lt;!–mep-nl–&gt; &lt;!–mep-tab–&gt;if (isset($qv[‘feed’]) &amp;&amp; !isset($qv[‘post_type’]))
<!–mep-nl–>&lt;!–mep-nl–&gt; &lt;!–mep-tab–&gt; &lt;!–mep-tab–&gt;$qv[‘post_type’] = array(‘post’, ‘books’, ‘movies’);
<!–mep-nl–>&lt;!–mep-nl–&gt; &lt;!–mep-tab–&gt;return $qv;
<!–mep-nl–>&lt;!–mep-nl–&gt;}
<!–mep-nl–>&lt;!–mep-nl–&gt;add_filter(‘request’, ‘myfeed_request’);
<!–mep-nl–>&lt;!–mep-nl–&gt;[/php]
Replace books and movies with your own custom post types.
Learn more about how to add custom post types to main WordPress RSS feed.
www.wpbeginner.com/wp-tutorials/how-to-add-custom-post-types-to-your-main-wordpress-rss-feed/
Search and Custom Post Types
SearchWP which allows you to create advanced search forms and is capable of searching through all custom post types, taxonomies, and custom fields.
If you would rather prefer to build your own custom search form, then here is what you need to do. First add your custom post types hidden fields in the search form by adding this code in searchform.php file of your child theme.
[php] <!–mep-nl–>&lt;!–mep-nl–&gt;&lt;input type="hidden" name="post_type[]" value="articles" /&gt;<!–mep-nl–>&lt;!–mep-nl–&gt;&lt;input type="hidden" name="post_type[]" value="post" /&gt;
<!–mep-nl–>&lt;!–mep-nl–&gt;&lt;input type="hidden" name="post_type[]" value="videos" /&gt;
<!–mep-nl–>&lt;!–mep-nl–&gt;&lt;input type="hidden" name="post_type[]" value="books" /&gt;
<!–mep-nl–>&lt;!–mep-nl–&gt;[/php]
This code simply adds hidden fields for your custom post types, replace value with your own custom post types. The next step is to tell WordPress what to do with these fields. Add this code before the loop in your theme’s search.php file:
[php] <!–mep-nl–>&lt;!–mep-nl–&gt;&lt;form role="search" method="get" id="searchform" action="&lt;?php echo home_url( ‘/’ ); ?&gt;"&gt;<!–mep-nl–>&lt;!–mep-nl–&gt;&lt;input type="text" name="s" id="s" &lt;?php if(is_search()) { ?&gt;value="&lt;?php the_search_query(); ?&gt;" &lt;?php } else { ?&gt;value="Enter keywords &amp;hellip;" onfocus="if(this.value==this.defaultValue)this.value=”;" onblur="if(this.value==”)this.value=this.defaultValue;"&lt;?php } ?&gt; /&gt;
<!–mep-nl–>&lt;!–mep-nl–&gt; &lt;!–mep-tab–&gt;
<!–mep-nl–>&lt;!–mep-nl–&gt;&lt;?php $query_types = get_query_var(‘post_type’); ?&gt;
<!–mep-nl–>&lt;!–mep-nl–&gt; &lt;!–mep-tab–&gt;
<!–mep-nl–>&lt;!–mep-nl–&gt;&lt;input type="checkbox" name="post_type[]" value="articles" &lt;?php if (in_array(‘articles’, $query_types)) { echo ‘checked="checked"’; } ?&gt; /&gt;&lt;label&gt;Articles&lt;/label&gt;
<!–mep-nl–>&lt;!–mep-nl–&gt;&lt;input type="checkbox" name="post_type[]" value="post" &lt;?php if (in_array(‘post’, $query_types)) { echo ‘checked="checked"’; } ?&gt; /&gt;&lt;label&gt;Blog&lt;/label&gt;
<!–mep-nl–>&lt;!–mep-nl–&gt;&lt;input type="checkbox" name="post_type[]" value="books" &lt;?php if (in_array(‘books’, $query_types)) { echo ‘checked="checked"’; } ?&gt; /&gt;&lt;label&gt;Books&lt;/label&gt;
<!–mep-nl–>&lt;!–mep-nl–&gt;&lt;input type="checkbox" name="post_type[]" value="videos" &lt;?php if (in_array(‘videos’, $query_types)) { echo ‘checked="checked"’; } ?&gt; /&gt;&lt;label&gt;Videos&lt;/label&gt;
<!–mep-nl–>&lt;!–mep-nl–&gt; &lt;!–mep-tab–&gt;
<!–mep-nl–>&lt;!–mep-nl–&gt;&lt;input type="submit" id="searchsubmit" value="Search" /&gt;
<!–mep-nl–>&lt;!–mep-nl–&gt;&lt;/form&gt;
<!–mep-nl–>&lt;!–mep-nl–&gt;[/php]
Chage post types
www.wpbeginner.com/plugins/how-to-convert-post-types/
Simply install and activate the Post Type Switcher plugin. Upon activation, go to Posts » All Posts. If it is a custom post type, then go to the screen that shows all items in that post type.
Using Custom Post Types as Taxonomies
Yes, you read that correctly, and you are probably thinking why use CPTs as taxonomies? Why not just create custom taxonomies? Let’s say that you have a custom post type for books and another custom post type for authors. Now you may want to associate authors with the books they have written. You can create a custom taxonomy for authors, but then you will have an authors taxonomy and a post type which only adds to the confusion.
wordpress.org/plugins/cpt-onomies/
Simply install and activate the CPT-onomies plugin. It allows you to build relationships between post types by using custom post types as taxonomy.
Using this plugin, the next time you add a book you can select the author as CPT-onomy and show all the books written by that author. Learn more about using custom post types as taxonomies.
www.wpbeginner.com/plugins/using-custom-post-types-as-taxonomies-in-wordpress-cpt-onomies/
Posts Table Pro – Full list of options
This is a complete list of all the options available in Posts Table Pro, with links to full instructions for each option.
You can use these to configure the [posts_table] shortcode in many different ways.
Table columns
columns – sets the columns in the table.
There are options to customize or remove the column headers
Choosing which posts appear in the table
There are lots of ways to create a table listing specific posts only:
category – lists posts from a specific category
tag – lists posts with a specific tag
post_type –
lists posts with a specific post type (e.g. products, events, or projects)
term –
lists posts with a specific custom taxonomy term
cf – lists posts with a specific custom field value
year – lists posts published in a specific year
month – lists posts published in a specific month
day – lists posts published on a specific day of the month
author – lists posts by a specific author/user
status – lists posts with a specific status (e.g. Published)
exclude – excludes specific posts from the table based on ID
include – lists specific posts in the table based on ID
exclude_category – excludes entire categories of posts from the table
rows_per_page – sets the number of posts on each page of the table
post_limit – sets the maximum number of posts that can appear in the table
Sorting & ordering
sort_by – controls how the posts are sorted when the table first loads
sort_order – sorts the table in ascending or descending order
filters – add filter dropdown lists above the table
search_term – filter by search term when the table first loads
Control elements above & below the table
search_box – shows, hides or positions the keyword search box
reset_button – shows, hides or positions the reset link
page_length – shows, hides or positions the “Show
totals – shows, hides or positions the post totals (e.g. “Showing 1 to 10 of 50 posts”)
pagination – shows, hides or positions the pagination buttons (previous, next etc)
paging_type – sets the pagination style
show_footer – shows or hides the footer row of the table
scroll_offset – changes the height that the page scrolls to when you move between pages in the table
Sizing & styling
widths – sets the width of each column
content_length – sets the number of characters in the ‘content’ column
excerpt_length – sets the number of characters in the ‘excerpt’ column
Images
image_size – sets the size of featured images in the table
lightbox – control whether your images open in a lightbox
Mobile visibility & responsive options
wrap – controls whether or not content is wrapped onto multiple lines
priorities – controls which columns are hidden on screen sizes when there are too many to fit on the page
column_breakpoints – provides fine-grained control over the breakpoints for each column when viewed on smaller screen sizes
responsive_control – controls the + icon which shows hidden rows
responsive_display – sets whether hidden rows are visible or hidden, or open in a modal window
Performance
lazy_load – improves performance by loading the overall page before the table, and loading 1 page of posts at a time
cache – whether to use caching to speed up table load time
Miscellaneous options
shortcodes – displays content generated by other shortcodes in the table (e.g. buttons or embedded audio or video players)
links – sets which columns in the table are clickable
search_on_click – controls whether clicking on a category, tag or custom taxonomy will filter the table or link to the relevant archive page
date_format – sets the date format for any date columns
date_columns – specifies which columns contain dates, so that the column can be sorted by date correctly
no_posts_message – controls the text that appears if no posts are found when the table first loads
no_posts_filtered_message – controls the text that appears if no results are found when a user searches or filters the table
numeric_terms – fixes any issues if you’re using numeric slugs for your categories or taxonomies
Posts table shortcode examples
To get you started, here are some popular examples of different ways to use Posts Table Pro.
Basic usage displaying all posts (see below for full list of columns):
[posts_table columns=”image,title,content,date,author”]
Display posts from a single category (use the category slug or ID):
[posts_table columns=”title,excerpt,tags” category=”news”]
Display posts from several categories. This example shows posts in the “ebooks” category or the “audiobooks” category: [posts_table category=”ebooks,audiobooks”]
Display posts that belong to several categories. This example shows posts that are in the “featured”, “course”, and “health” categories:
[posts_table columns=”name,short-description,tags,price,add-to-cart” category=”featured+course+health”]
Exclude posts from a category. This example excludes posts in the “clients” category:
[posts_table exclude_category=”clients”]
Display featured image. Defaults to 50 by 50 pixels, but you can specify a custom image size if you wish:
[posts_table columns=”image,title,content” image_size=”80×80″]
Choosing custom column headings:
[posts_table columns=”title:Post Name,content:Description,Author:Written By”]
What else can I do?
Please also see our articles on advanced usage, which include advice on adding tables to category archive or search results pages, translating the posts table into other languages, developer documentation, and more. We’ve also published a list of compatible third party plugins that you can use with Posts Table Pro.
code.tutsplus.com/series/mastering-wp_query–cms-818
this is what is driving it –
Plugins::
www.nsp-code.com/premium-plugins/
www.nsp-code.com/advanced-post-types-order-api/
barn2.co.uk/wordpress-plugins/posts-table-pro/
wplift.com/wordpress-document-library-plugin
Conductor::