• Hey all!

    I’ve got a question I want to do the following:
    Generate page with all the posts sorted per category and then per custom field.
    I’ve got the following code as a function for a shortcode.

    Now I already know how to do this per category. I can split the posts per category and give is a nice title per category. But now I need to know how to do the same per pre-divined custom field. Say, “customfield_01”.

    	<?php
    	global $post;
    
    	$categories_array = array();
    	$thecategories = get_categories();
    	foreach( $thecategories as $category ){
    		$categories_array[] = $category->slug;
    	}
    
    	if($atts[ 'category' ]){
    		$atts[ 'category' ] = explode( ",", $atts[ 'category' ] );
    	}
    
    	//collect values, combining passed in values and defaults
    	$values = shortcode_atts(array(
    			'category' => ''
    		),$atts);
    
    	$categories = get_categories( array(
    			'orderby' => 'name',
    			'parent'  => 0,
    			'slug'    => $values['category']
    		) );
    
    	$current = get_the_ID($post->ID);
    
    	foreach ( $categories as $tax ) :
    
    		// List posts by the terms for a custom taxonomy of any post type
    		$args = array(
    			'post_type' => 'products',
    			'orderby' => 'ASC',
    			'posts_per_page'=>-1,
    			'category_name' => $tax->slug
    		);
    		$the_query = new WP_Query( $args ); ?>
    			<?php if ( $the_query->have_posts() ) : ?>
    				<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    					<!-- magic -->
    					
    				<?php endwhile; ?>
    				<?php wp_reset_postdata(); ?>
    			<?php endif; ?>
    	<?php endforeach; ?>
    

    Looking at this code I get the feeling I first need to make an array with all the different values of field “customfield_01” of the current category.

    Does anyone know how to do this?
    Or maybe even more efficient? ??

    Many many thanks for any suggestions !

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    It’s not clear what you’re after. You have a number of categories, with posts listed in each category, ordered by date, oldest first right now.

    Do you want the posts in each category ordered by the value of “customfield_01”? Or actually grouped by each value like the categories are grouped? Are other fields going to factor into this? Or the one field is it?

    Are the field values from a known, limited set, or are they open ended? An example of your desired output under one category would be helpful.

    Thread Starter TDR

    (@tdr)

    Pardon me, where are my manners. Somehow I didn’t see I got a reply ??
    Thanks for your reply!

    Yes, group per category and than group per “customfield_01”.
    I’ve found a solution. I’ll post it below.

    I’ve added this code to the code above. I’ve replaced all the code within “foreach ( $categories as $tax ) :”

    Steps:
    – needed an array with all the unique values within “field 1”
    – ran an post query for each post, grabbed the value of “field 1” and put in an array
    – filtered all the duplicates
    – alphabetized the array
    – run a foreach in the array:

    $stack = array();
    
    // query
    $args = array(
        'post_type' => 'products',
        'orderby' => 'ASC',
        'posts_per_page'=>-1,
        'category_name' => $tax->slug
    );
    
    // The Query
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
    
            if( get_field('custom_field_1') ): 
                $stack[] = get_field('custom_field_1');
            endif;
        }
        wp_reset_postdata();
    }   
    
    $result = array_unique($stack);
    sort($result);
    
    foreach ( $result as $tax2 ) :
         // run you code here
    endforeach; 

    Might not be the prettiest code, but it gets the job done ??

    Moderator bcworkz

    (@bcworkz)

    Job done is what matters. Thanks for reporting back.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Nested Post Query, first catagory, then per ACF’ is closed to new replies.