• Hello,

    I’m trying to regroup posts containing different meta values & to display only last posts written for each meta value.

    So I wrote this piece of code :

    add_filter('posts_groupby', 'query_group_by_filter');
     	function  query_group_by_filter($groupby){
           global $wpdb;
           return $wpdb->postmeta . '.meta_value';
        }
    
    	// WP_Query arguments
    	$args = array (
    		'post_type' => 'post',
    		'post_status' => 'publish',
    		'order' => 'DESC',
    		'orderby' => 'date',
    		'meta_query' => array(
    		      array(
    		      'key' => 'article_information_artiste',
    	       	),
    	    ),
    	);
    
    	// The Query
    	$query = new WP_Query( $args );
    	// The Loop
    	if ( $query->have_posts() ) {
    		while ( $query->have_posts() ) {
    			$query->the_post(); ?>
    			// Do something
    		<?php }
    	} else {
    		echo "No content!";
    	}
    
    	remove_filter('posts_groupby', 'query_group_by_filter');
    	// Restore original Post Data
    	wp_reset_postdata();

    It regroups posts with specific meta values but it displays only the first post, not the last one.

    I try to add to the wp_query order & orderby argument but nothing happens.

    Do you know how to display only last post written for each meta value ?
    Here a preview

    Thanks for your help.

  • The topic ‘Display last posts with wp_query using meta value & groupby’ is closed to new replies.