How to Display WordPress Posts From Category Within a Custom Taxonomy?

How to Display WordPress Posts From Category Within a Custom Taxonomy?

How to Display WordPress Posts From Category Within a Custom Taxonomy?

400x166 At first you need register a custom_post_type  in your functions.php file. So that your custom post type are display in wordpress admin panel. in functions.php file just past this function to register a custom_post_type

Here we create a custom post type as 'features' post

/*	- Features Section
===============================================*/
function features_custom_post() {
$labels = array(
'name'                => _x( 'Features Catalog', 'Post Type General Name', 'raleway' ),
'singular_name'       => _x( 'Feature Catalog', 'Post Type Singular Name', 'raleway' ),
'menu_name'           => __( 'Features Catalog', 'raleway' ),
'name_admin_bar'      => __( 'Features Catalog', 'raleway' ),
'parent_item_colon'   => __( 'Features', 'raleway' ),
'all_items'           => __( 'All Features', 'raleway' ),
'add_new_item'        => __( 'Add New Feature', 'raleway' ),
'add_new'             => __( 'Add New Feature', 'raleway' ),
'new_item'            => __( 'New Features', 'raleway' ),

);
$args = array(
'label'               => __( 'features', 'raleway' ),
'description'         => __( 'Features', 'raleway' ),
'labels'              => $labels,
'supports'            => array( 'title',  'thumbnail', 'custom-fields', 'page-attributes', 'editor'),
'taxonomies'          => array( 'filter' ),
'hierarchical'        => false,
'public'              => true,
'show_ui'             => true,
'show_in_menu'        => true,
'menu_position'       => 5,
'menu_icon'           => 'dashicons-welcome-widgets-menus',
'show_in_admin_bar'   => true,
'show_in_nav_menus'   => true,
'can_export'          => true,
'has_archive'         => true,
'exclude_from_search' => false,
'publicly_queryable'  => true,
'capability_type'     => 'page',
);
register_post_type( 'features', $args );
}
add_action( 'init', 'features_custom_post', 0 );
/* - Features Section
================================================*/


Second: You must create a tanonomy function so that you create a category where you can post different category. in our category we given a name as "Filter"

NB: You should make same name on post_register taxonomies name to taxonomy_register name such as

post_register_type  : 'taxonomies'=> array( 'filter' ),

taxonomy_register : register_taxonomy( 'filter', array( 'features' ), $args );


Here our category or taxonomy name is "filter" that are used in both function that is same,

it will create a relation to create a category under 'features' post

and "features" is the post type that is come form post_register function

/*	- Features filter taxonomy
========================================================*/
function custom_taxonomy_features() {
$labels = array(
'name'                       => _x( 'Filter', 'Taxonomy General Name', 'winbizdigital' ),
'singular_name'              => _x( 'Filter', 'Taxonomy Singular Name', 'winbizdigital' ),
'menu_name'                  => __( 'Filter', 'winbizdigital' ),
'all_items'                  => __( 'All Filter', 'winbizdigital' ),
'parent_item'                => __( 'Parent Filter', 'winbizdigital' ),
'parent_item_colon'          => __( 'Parent Filter:', 'winbizdigital' ),
'new_item_name'              => __( 'New Filter', 'winbizdigital' ),
'add_new_item'               => __( 'Add New Filter', 'winbizdigital' ),
'edit_item'                  => __( 'Edit Filter', 'winbizdigital' ),
'update_item'                => __( 'Update Filter', 'winbizdigital' ),
'view_item'                  => __( 'View Filter', 'winbizdigital' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'winbizdigital' ),
'add_or_remove_items'        => __( 'Add or remove items', 'winbizdigital' ),
'choose_from_most_used'      => __( 'Choose from the most used', 'winbizdigital' ),
'popular_items'              => __( 'Popular Filter', 'winbizdigital' ),
'search_items'               => __( 'Search Filter', 'winbizdigital' ),
'not_found'                  => __( 'Not Found', 'winbizdigital' ),
);
$args = array(
'labels'                     => $labels,
'hierarchical'               => true,
'public'                     => true,
'show_ui'                    => true,
'show_admin_column'          => true,
'show_in_nav_menus'          => true,
'show_tagcloud'              => true,
);
register_taxonomy( 'filter', array( 'features' ), $args );
}
// Hook into the 'init' action
add_action( 'init', 'custom_taxonomy_features', 0 );
/* - Features Section End
=======================================*/


Now after creating our post type and pot taxonomy in our wp admin panel we will see as blew and we will create some category or filter as blew image.






So we are ready to Display wp Posts From Category Within a Custom Taxonomy using blew function..

<?php

$the_query = new WP_Query( array(

'post_type' => 'features',

'posts_per_page'=> 1,

'tax_query' =>array(

array (

'taxonomy' => 'filter', //taxonomy register name

'field' => 'slug', // showin field name / id

'terms' => 'first-catalog', // show by slug name

)

),

) );

while($the_query->have_posts() ):$the_query->the_post(); ?>


             // write your html view code here


<?php endwhile; wp_reset_postdata(); ?>


NB: Here we display post from features post to filter taxonomy using slug name as terms: first-catalog

in this way you can show different post from different category or filter


Thanks for read if you fill it is helpful please share this post with your friends.

  • wp
  • WordPress taxonomy
  • custom post register
  • Display Posts From Category
  • Display Posts From Category to Taxonomy
  • display post from specific category
  • Comments