Support » Fixing WordPress » New WP Query Multiple Loops

  • Resolved imaginocracy

    (@imaginocracy)


    I am trying to run multiple loops on a page template that pull a custom post type from categories and display the title, the content, and the custom metabox data that I created for the post. This is for a restaurant, and they will have a page (dinner menu) then I want to use multiple loops to pull categories for appetizers, soups, entrees, etc in order.

    <main class="menu">
     <div class="row">
      <div class="span8 menu-entry dinner-menu">
      <h1>Savannah's Dinner Menu</h1>
        <?php // Appetizers
        $the_query = new WP_Query( 'category_name=appetizer' );
    
        // The Loop
        if ( $the_query->have_posts() ):
        while ( $the_query->have_posts() ) :
            $the_query->the_post(); ?>
    
        <h2><?php the_title(); ?></h2>
        <p><?php the_content(); ?></p>
    
        <p class="price"> <?php post_meta($post->ID, 'price-box', true); ?></p>
    
       <?php endwhile; endif;
    
       wp_reset_postdata(); ?>
    
    <?php // Appetizers
        $query2 = new WP_Query( 'category_name=soups-and-salads' );
    
        // The Loop
        if ( $query2->have_posts() ):
        while ( $query2->have_posts() ) :
            $query2->the_post(); ?>
    
        <h2><?php the_title(); ?></h2>
        <p><?php the_content(); ?></p>
    
        <p class="price"> <?php post_meta($post->ID, 'price-box', true); ?></p>
    
    <?php endwhile; endif;
    
       wp_reset_postdata(); ?>
    
      </div>
     </div>
    </main>

    Currently this does not display any information in the <main> section, though there are no errors reported either. Not sure where I am going wrong.

    Thanks for any help,

    Bill

Viewing 2 replies - 1 through 2 (of 2 total)
  • J M

    (@hiphopinenglish)

    When you use category_name you are asked to use IDs and NOT slugs (see the Codex).
    That may help.
    You could also see Bill Erickson’s excellent guide to using wp_query with all possible parameters.
    You could also use Generate WP.

    Thread Starter imaginocracy

    (@imaginocracy)

    So because I am using a custom post type you need to add the ‘post_type’ argument to the new WP_Query in addition to the category name. The other issue is that I was calling the meta box by div ID not by the actual meta ID.

    Resolved!

    <main>
     <div class="row">
      <div class="span8 menu-entry dinner-menu paddedLR">
      <h1 class="menu-title">Savannah's Dinner Menu</h1>
      <h2 class="menu-sub-head">Appetizers</h2>
        <?php // Appetizers
        $the_query = new WP_Query( array( 'post_type' => 'dishes', 'category_name' => 'appetizer' ));
    
        // The Loop
        if ( $the_query->have_posts() ):
        while ( $the_query->have_posts() ) :
            $the_query->the_post(); ?>
        <div class="menu-item-wrapper appetizer-item">
        <h2 class="menu-item-title"><?php the_title(); ?></h2>
        <?php the_content(); ?>
    
        <span class="price">
          <?php
          $price_value = get_post_meta($post->ID, 'PRICE_text', true);
          if ( ! empty( $price_value ) ) {
            echo $price_value;
          }
          ?>
        </span>
      </div><!--/.appetizer -->
       <?php endwhile; endif;
    
       wp_reset_postdata(); ?>
    
       <h2 class="menu-sub-head">Soups & Salads</h2>
       <?php // Soups & Salads
        $query2 = new WP_Query( array( 'post_type' => 'dishes', 'category_name' => 'soups-and-salads' ));
    
        // The Loop
        if ( $query2->have_posts() ):
        while ( $query2->have_posts() ) :
            $query2->the_post(); ?>
    
        <h2><?php the_title(); ?></h2>
        <p class="item-description"><?php the_content(); ?></p>
        <p class="price">
          <?php
          $price_value = get_post_meta($post->ID, 'PRICE_text', true);
          if ( ! empty( $price_value ) ) {
            echo $price_value;
          }
          ?>
        </p>
    
        <?php endwhile; endif;
    
       wp_reset_postdata(); ?>
    
      </div>
     </div>
    </main>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘New WP Query Multiple Loops’ is closed to new replies.