Show all posts from a custom post type
WordPress has a small function get_posts() to do this job. You just need to set your arguments and pass that argument inside the function. It returns an array of latest posts. You can use foreach function to loop though the returned array.
$args = array(
'post_type' => 'cpt-slug',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1,
);
$posts = get_posts( $args );
Show all posts for a specific category of a custom post type
If you are developing a theme or plugin you might have face this type of things. Some sort of taxonomy query needed to do this job. It will return an object. With that object you can run your loop and show your post from that loop.
$query_args = array(
'post_type' => 'cpt-slug',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'cpt-taxonomy-slug',
'field' => 'term_id',
'terms' => 'ctp-term-id',
),
),
);
$my_query = new WP_Query( $query_args );
With pagination
If you want to show pagination you need to pass one more parameter into your arguments. You just need to pass paged
parameter inside your query.
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query_args = array(
'post_type' => 'cpt-slug',
'posts_per_page' => -1,
'paged' => $paged
'tax_query' => array(
array(
'taxonomy' => 'cpt-taxonomy-slug',
'field' => 'term_id',
'terms' => 'ctp-term-id',
),
),
);
$my_query = new WP_Query( $query_args );
Show category for single post
To show category for single post, there is a default function get_the_terms() from WordPress to do this job. You just need to pass single post ID and taxonomy slug for the category. It will return an array of assigned category for that single post.
get_the_terms( $post_id, 'taxonomy-slug' )
Get all category list
If you want to get all lists of your category for your registered taxonomy, get_terms() is the right function to do this job. You just need to pass taxonomy slug and your arguments for the query. This will return an array of all list of your category.
$terms = get_terms( 'taxonomy-slug', array(
'hide_empty' => false,
) );
Show child categories of a parent category
If you have some child category of a parent category, then it might be needed to get all list of sub categories of a parent categories. WordPress default function get_terms() is perfect to do this job. Its returns an array of object for all sub categories.
$sub_categroy_query_args = array(
'parent' => $parent_id,
);
$sub_categories = get_terms( 'taxonomy-slug', $sub_categroy_query_args );
Remove specific category post from WP Query
For theme development or plugin development purpose, sometime you need to remove any specific category post from WP Query. To do this type of job, you just need to pass those category ID in the Category__not_in
array.
$my_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'category__not_in' => array( cat_id_1, cat_id_2 ),
'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
);
$post_query = new WP_Query( $my_args );
Show archive post according to menu order
When you need control over the order of archive post how it currently showing, in that case menu order comes into play. With the help of this property you can set order in the post edit screen. Then your archive page post will be display according to the order your set for the posts. You need to add menu order in the query loop like below.
$qobj = $wp_query->get_queried_object();
$solution_tax_args = array(
'post_type' => 'cpt-type',
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => 'ASC',
'ignore_sticky_posts' => 1,
'posts_per_page' => -1,
'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
'tax_query' => array(
array(
'taxonomy' => $qobj->taxonomy,
'field' => 'id',
'terms' => $qobj->term_id,
)
)
);