I want to fetch all categories and the post that belongs to the category, to fetch all categories, I am using the following code

$cat_args = array(
    'taxonomy'      => 'article-category',
    'orderby'       => 'menu_order',
    'order'         => 'ASC',
    'hierarchical'  => true,
    'parent'        => 0,
    'hide_empty'    => true,
    'child_of'      => 0
$get_categories = get_categories( $cat_args );

To fetch all post with type articles I am using this code

$query = new WP_Query(array(
    'post_type' => 'article',
    'post_status' => 'publish',
    'posts_per_page' => -1,
while ($query->have_posts()) {
    $post_id = get_the_ID();

This gives me the list of all categories and list of all post of type articles in a separate loop, however what I want is something in this line

Category 1
    - Post 1 Title
    - Post 2 Title
    - Post 3 Title
Category 2
    - Post 4 Title
    - Post 5 Title
    - Post 6 Title
    - Post 7 Title

I am basically trying to fetch all categories and all post of type article that belongs to this category, I tried many different ways, but nothing seems to work out, how to go about it?


Read more here: WordPress – Get Categories & Posts With Type Article


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: