1

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();
        };?>
  • can you please show me one shortcode you have use for this. – Tanmay Patel Jun 24 at 4:44
1

These are the issues I see in your code:

  1. You should use empty( $category ) instead of ! isset( $category ):

    // Sample shortcode: [himenu]
    if ( ! empty( $menu_categories ) && empty( $category ) ) {
        foreach ( $menu_categories as $menu_category ):
            $args = [
                'post_type'      => 'menu_item',
                'posts_per_page' => -1,
                'order'          => 'ASC',
                'orderby'        => 'title',
                //'category_name' => $category, // remove this
                'cat'            => $menu_category->term_id
            ];
    
            $query = new WP_Query( $args );
            // ... your code here.
            wp_reset_postdata();
        endforeach;
    }
    
  2. And in the else part, just omit the 'cat' => $menu_category->term_id:

    // Sample shortcode: [himenu category="foo"]
    elseif ( ! empty( $category ) ) {
        $args = [
            'post_type'      => 'menu_item',
            'posts_per_page' => -1,
            'order'          => 'ASC',
            'orderby'        => 'title',
            'category_name'  => $category,
        ];
    
        $query = new WP_Query( $args );
        // ... your code here.
    }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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