Support

Account

Home Forums General Issues Displaying grouped content

Helping

Displaying grouped content

  • Hi.. I created some a custom field group for Barbecues with some basic details. Brand, image, description. Able to enter and display my data however I can’t seem to figure out how to group the output by brand. Sure I can hard code a loop for each brand but I’d like it to:

    1) count the number of brands,
    2) display the 1st brand products
    3) display the 2nd brand products
    4) etc..

    Code I have now is:
    $args = array(
    ‘post_type’ => ‘patio’,
    ‘meta_key’ => ‘brand’,

    );

    $query = new WP_Query($args);

    Then:
    <?php if ($query->have_posts() ) : while($query->have_posts() ) : $query->the_post(); ?>

    //displays product info.

    <?php endwhile; endif; wp_reset_postdata(); ?>

  • This isn’t something that ACF will do automatically, you’ll need to handle this with your query and loop.

    WP 4.0 and 4.2 introduced better ordering.

    For the latest change, see this post: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

    As for the enhanced orderby see the orderby section codex page for WP_Query or this post: https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/

    First you need to get them ordered correctly. This will output them in the right order, then you can create a custom loop that will do the work.

    Here is an example of a simple loop that will do the separating.

    
    $last_brand = '';
    $this_brand = '';
    while (have_poste()) {
        the_post();
        $this_brand = get_field('brand');
        if ($this_brand != $last_brand) {
            // change brands
        }
        $last_brand = $this_brand;
        // rest of the loop here
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Displaying grouped content’ is closed to new replies.