Support

Account

Home Forums Front-end Issues Grouping Results by Field on the Front-end

Solved

Grouping Results by Field on the Front-end

  • Hi,

    I have a hierarchical CPT, I am pulling in all the children of a page. These child pages have a field called “nldf_category_type”. The category types are dynamic and there can be any number of them. Is it possible to list all of the categories and under each category name list the pages that have that category? I am trying to create an archive of all the child pages grouped together by category type.

    Here is what I have so far, it just returns all of the child pages without any grouping.

    $args = array(
    	    'post_type'      => 'nldf',
    	    'posts_per_page' => -1,
    	    'post_parent'    => $post->ID,
    	    'meta_key'		 => 'nldf_category_type',
    	    'order'          => 'ASC',
    	    'orderby'        => 'menu_order'
    	);
    
    	$parent = new WP_Query( $args );
    
    	if ( $parent->have_posts() ) :
    	    while ( $parent->have_posts() ) : $parent->the_post(); 
    	?>
    	        <div id="parent-<?php the_ID(); ?>" class="parent-page">
    	            <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
    	            <p><?php the_content(); ?></p>
    				<?php 
    	            $category = get_field('nldf_category_type', $page->ID);
    				echo $category;
    				?>
    	        </div>
    	<?php 
    		endwhile;
    	endif; 
    	wp_reset_query(); 

    Thanks for any help!

  • Here is the CPT code, just in case.

    function nldf_gallery() {
        $labels = array(
            'name'               => 'NLDF',
            'singular_name'      => 'NLDF',
            'add_new'            => 'Add New',
            'add_new_item'       => 'Add New NLDF',
            'edit_item'          => 'Edit NLDF',
            'new_item'           => 'New NLDF',
            'all_items'          => 'All NLDF',
            'view_item'          => 'View NLDF',
            'search_items'       => 'Search NLDF',
            'not_found'          => 'No NLDF found',
            'not_found_in_trash' => 'No NLDF found in Trash',
            'menu_name'          => 'NLDF'
        );
    
        $args = array(
            'labels'             => $labels,
            'public'             => true,
            'hierarchical'       => true,
            'has_archive'        => false,
            'menu_position'      => 4,
            'menu_icon'          => 'dashicons-format-gallery',
            'supports'           => array('title','editor','comments','revisions','page-attributes')
        );
    
        register_post_type('nldf', $args);
    }
    add_action('init', 'nldf_gallery');
  • Hi @jeathree

    There are two methods that I can think for your case.

    First, you can loop trough the available categories and query the posts based on it like this:

    $args = array(
            'post_type'      => 'nldf',
            'posts_per_page' => -1,
            'post_parent'    => $post->ID,
            'meta_key'         => 'nldf_category_type',
            'meta_value'     => $the_category,
            'order'          => 'ASC',
            'orderby'        => 'menu_order'
        );

    Where $the_category is the category in the loop.

    The second method, you can get the posts by using the get_posts() function instead of the WP_Query class and then loop trough the posts and group it accordingly in an array. Maybe something like this (I assume that ‘nldf_category_type’ is a taxonomy field):

    // Get all of the posts
    $posts_array = get_posts( $args );
    
    // Create a new array
    $grouped_posts = array();
    
    // Group it in a new array
    foreach( $posts_array as $the_post ) {
        $category = get_field('nldf_category_type', $the_post->ID);
        
        // If the category group is not created yet, create it!
        if( !isset($grouped_posts[$category->name]) ){
            $grouped_posts[$category->name] = array();
        }
        
        // Put the post in the group
        $grouped_posts[$category->name][] = $the_post;
        
    }
    
    // Loop it!
    foreach( $grouped_posts as $cat_name => $the_posts ) {
        echo $cat_name;
        foreach( $the_posts as $the_post ){
            echo $the_post->title;
        }
    }

    I hope this helps 🙂

  • Thanks for the response! I am trying the second method, I think it might be close.

    I changed the “nldf_category_type” field to be a taxonomy field and created a custom taxonomy, here is the code for that.

    register_taxonomy(
            'nldf_category',
            'nldf',
            array(
                'label' => 'Category',
                'hierarchical' => false,
                'rewrite' => array( 'slug' => 'group', 'with_front' => true, 'hierarchical' => false),
                'show_in_menu' => false,
                'show_ui' => false
            )
        );

    While troubleshooting here are some things I noticed, for some reason in both loops nothing is getting echo’d out, put if I put in echo “test” it works each time through the loop. Also if var_dump($grouped_posts); I see no mention of any category. And if I echo $category I just get 33223, I am guessing those are IDs of the 2 categories?

    Hmmm, I also just noticed that echo $category->name; gives me nothing.

    I will keep tinkering and see what I can do, probably nothing 🙂

  • All right, I think I got it 🙂 I had to change the taxonomy field to return an object, I didnt catch that in the docs.

    One last question.

    Is there another way of getting the url of the child page in the loop besides using echo $the_post->guid;? That is returning the non permalink like this -> ?post_type=nldf&p=199.

    Thanks for all the help! I’ve learned a lot!

  • Hi @jeathree

    I think you can use the get_permalink() function like this:

    echo get_permalink( $the_post->ID );

    There are a lot of functions which you can use to get more information from a post by passing the ID. Please take a look at the Codex to learn more about them.

    I hope this helps 🙂

  • I am all set! Thanks again!

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

You must be logged in to reply to this topic.