Support

Account

Home Forums ACF PRO meta_query doesn't work for filter a query by an ACF value and order by another

Helping

meta_query doesn't work for filter a query by an ACF value and order by another

  • Hi, I have to create a query for a custom post type, filter than with an ACF value (radio button), group the results and sort it by date order with another ACF value (year).

    See the link to view a prototype: https://recordit.co/VPEJadMyUW

    The webpage is an advanced portfolio for an artist. The specific section is formed by his expositions.

    I try to use the instructions in another forum post (👉https://bit.ly/3r25uN3) without success, it doesn’t work. Meta-query doesn’t work, only simple query.

    As I said above, the page is an index of expositions, grouped and filtered by year, as you see in the link.

    This is my code with meta_query, that not working:

    <?php 
    			$args = array(
    				'post_type'			=> 'exposiciones', 
    				'numberposts'		=> -1,
    				'meta_query'		=> array(
    					'relation'		=> 'AND',
    					array(
    						'key'	 	=> 'expo_type',
    						'value'	  	=> 'individual',
    						'compare' 	=> 'IN',
    					),
    					array(
    						'key'	  	=> 'expo_start_date',
    						'value'	  	=> '1',
    						'compare' 	=> '=',
    					),
    				),
    				'meta_key'			=> 'expo_start_date',
    				'orderby'			=> 'meta_value',
    				'order'				=> 'DESC'
    			);
    
    			
    			
    			// query
    			$the_query = new WP_Query( $args );
    
    			?>
    			<?php if( $the_query->have_posts() ): ?>
    
    				<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    				<div class="expo-header-year">
    							<?php the_field('expo_start_date'); ?>
    				</div>
    						<div class="expo-index-data-container">
    							<a href="<?php the_permalink(); ?>">
    
    								<div class="expo-index-data">
    									<h4><?php the_field('expo_title'); ?></h4>
    									<?php the_field('expo_institute'); ?>
    
    									<?php the_field('expo_where'); ?>
    
    								</div>
    
    								<div class="expo-index-images-container">
    								<?php 
    									$images = get_field('expo_images');
    									$size = 'large'; 
    									if( $images ): ?>
    										<ul>
    											<?php foreach( $images as $image_id ): ?>
    												<li>
    													<?php echo wp_get_attachment_image( $image_id, $size ); ?>
    												</li>
    											<?php endforeach; ?>
    										</ul>
    									<?php endif; ?>
    								</div>
    
    							</a>
    						</div>
    					<?php endwhile; ?>
    				<?php endif; ?>
    
    				<?php wp_reset_query(); ?>

    I can create a query of custom post type (‘exposiciones’), then filter it by ACF field (expo_type) and his value (‘individual’), but I can’t group posts of the same year bottom of the year title, and mainly ordered from the most recent to the most ancient value.

    Thanks for your collab.

  • Hi, Noone responds but I try to use pre_get_posts:

    function my_pre_get_posts( $expo_query  ) {
    	
    	// do not modify queries in the admin
    	if( is_admin() ) {
    		
    		return $expo_query ;
    		
    	}
    	
    
    	// only modify queries for 'exposiciones' post type
    	if( isset($expo_query ->query_vars['post_type']) && $expo_query ->query_vars['post_type'] == 'exposiciones' ) {
    		
    		$expo_query ->set('meta_key', 'expo_start_date');	 
    		$expo_query ->set('orderby', 'meta_value');	
    		$expo_query ->set('order', 'DESC'); 
    		
    	}
    	
    
    	// return
    	return $expo_query ;
    
    }
    
    add_action('pre_get_posts', 'my_pre_get_posts');

    without success. 🙁

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

You must be logged in to reply to this topic.