Support

Account

Home Forums Front-end Issues WP_query for each loop

Solving

WP_query for each loop

  • Hi there, been trying to figure this out for a while and can’t work it out or find any examples…

    I have a CPT (tools) with two setups of Radio Buttons. I need to run a query to get all posts in the 1st button field (calculator) and then group them by the 2nd button field (wheel section).

    
    <?php    
                        $args = array(
                          'posts_per_page' => -1,
                          'post_type'   => 'tools',
                          'meta_key'  => 'wheel_section', // (2nd button - group by)
                          'orderby'   => 'meta_value',
                          'meta_query'  => array(
                              'key'   => 'tool_type',
                              'value'   => 'Calculator', // (1st button - show only)
                              'compare' => '='
                            ),  
                        );
    $the_query = new WP_Query( $args );
    ?>

    The usual would be to then
    <?php if( $the_query->have_posts() ): ?>

    followed by
    <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>

    How would group all items by button 2 with a heading?

  • What you need to do is look at the value for wheel_section and compare it to the value of the last post to see if you need to start a new group

    
    if (have_posts()) {
      $last_wheel_section = '';
      while(have_posts()) {
        the_post();
        $this_wheel_section = get_field('wheel_section');
        if ($this_wheel_section != $last_wheel_section) {
          // new section started
          // this will always be true for the first section
        }
        
        // other code for each wheel section
    
        // at end of loop set $last_wheel_section to $this_wheel_section
        $last_wheel_section = $this_wheel_section
      } // end whild have_posts
    } // end if have_posts
    
  • Think that I asked the wrong question, or didn’t put all info (!duh).

    I have two CPTs: Products & tools. In Products you can select related tools using the relationship field.

    The tools have a radio button (wheel_section). On the product page I want to get all relationships and display them in groups based on the value of wheel_section.

    Been searching, racking my brain, biting my nails, etc. but cannot work out how to do this. This is how far I have got – if anyone can point me in the right direction would be very grateful.

    1) Get related posts
    <?php $posts = get_field('product_tools'); if( $posts ): ?>

    2) Look through posts / setup data

    <?php $i=0; foreach( $posts as $post): ?>
     <?php setup_postdata($post); ?>

    3) Get choices from wheel_section radio button
    <?php echo implode(', ', get_field_object('wheel_section')[choices]); ?>

    4) Loop through choices
    This is where I get lost. Now I have an array and I need to loop through each choice and get corresponding label and calculators so I get:

    WHEEL SECTION LABEL 1
    – calculators in wheel section 1

    WHEEL SECTION LABEL 2
    – calculators in wheel section 2

  • The reason that I think your having difficulty doing what your trying to do is that you’re trying to group the posts after you get the posts.

    get posts => look at field and => group the posts by the field

    Once you’ve gotten the posts and started looping through them it is too late to group them in a specific order. You need to set the order/grouping before you get them.

    To compound things further, your not only want them ordered by this field, but you also want them ordered in the same order as the field choices set in the ACF field. Depending on what this field holds it may not even be possible to order them.

    So, a couple of questions.

    Are the choices set in the field choices in alphanumeric order?

    Can the wheel_section field have more than one value?

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘WP_query for each loop’ is closed to new replies.