I have a Query that goes in and should list all the categories for a post type and loop through and display all the posts within that category but I can't get it to work, it shows the posts in a seemingly random order?
It should, if there is no category set in the shortcode atts, just grab all the categories and list all the posts i.e.
KEBABS (category)
Post 1
Post 2
Post 3
SIDES (category)
Post 4
Post 5
Post 6
Any advice would be welcome. Thanks
//MENU SHORTCODE
add_shortcode('himenu', 'create_menu_shortcode');
function create_menu_shortcode ($atts){
$atts = shortcode_atts(
array(
'category' => '',
'posts_per_page' => ''
), $atts, 'himenu' );
$category = $atts["category"];
?> <div class=hi-menu-box><?php
// var_dump ($atts);
// var_dump ($category);
$args_cat = [
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
];
$menu_categories = get_categories($args_cat);
print_r($menu_categories);
if (!empty($menu_categories) && !isset($category)){
foreach ($menu_categories as $menu_category):
$args = [
'post_type' => 'menu_item',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
'category_name' => $category,
'cat' => $menu_category->term_id
];
$query = new WP_Query($args);
while ($query->have_posts()) : $query->the_post(); ?>
<div class="indi-menu-item">
<div class="menu-item-column">
<h5> <?php
if($category === 'special-offers'){ echo "Special Offers";} else{the_category();} ?></h5>
<div class="menu-item-meta">
<h4> <?php the_title(); ?></h4>
<p><?php the_field('description')?></p>
</div>
</div>
<div class="menu-item-column">
<h2>£<?php the_field('price'); ?></h2>
</div>
</div>
<?php endwhile;
wp_reset_postdata();
endforeach;}
else{
$args = [
'post_type' => 'menu_item',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
'category_name' => $category,
'cat' => $menu_category->term_id
];
$query = new WP_Query($args);
while ($query->have_posts()) : $query->the_post(); ?>
<div class="indi-menu-item">
<div class="menu-item-column">
<h5> <?php
if($category === 'special-offers'){ echo "Special Offers";} else{the_category();} ?></h5>
<div class="menu-item-meta">
<h4> <?php the_title(); ?></h4>
<p><?php the_field('description')?></p>
</div>
</div>
<div class="menu-item-column">
<h2>£<?php the_field('price'); ?></h2>
</div>
</div>
<?php endwhile;
};?>
</div>
<?php wp_reset_query();
};?>