0

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()) {
    $query->the_post();
    $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?

Thanks.

1
  • 1
    You can try by the tax_query. This questions answer can help you.
    – Mahafuz
    Apr 21, 2017 at 13:24

1 Answer 1

0

I solved this with following code:

$currentCategoryId = wp_get_object_terms($post->ID, 'article-category', array('fields' => 'ids'));
if (is_array($currentCategoryId) && !empty($currentCategoryId[0])) {
    $currentCategoryId = (int)$currentCategoryId[0];
}

$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 );
$categories = array();
foreach ($get_categories as $index => $category) {
    $categories[$index]['id'] = $category->cat_ID;
    $categories[$index]['slug'] = $category->slug;
    $categories[$index]['name'] = $category->name;
}
$articles = array();
foreach ($categories as $index => $category) {
    $args = array(
        'post_type'   => 'article',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'tax_query' => array(
            array (
                'taxonomy' => 'article-category',
                'field' => 'slug',
                'terms' => $category['slug'],
            )
        )
    );
    $articles[$index]['category']['id'] = $category['id'];
    $articles[$index]['category']['name'] = $category['name'];
    $articles[$index]['category']['slug'] = $category['slug'];
    $posts_array = get_posts( $args );
    foreach ($posts_array as $key => $_post) {
        $articles[$index]['posts'][$key]['id'] = $_post->ID;
        $articles[$index]['posts'][$key]['title'] = $_post->post_title;
        $articles[$index]['posts'][$key]['permalink'] = get_permalink($_post->ID);
    }
}
wp_reset_postdata();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.