Support

Account

Home Forums ACF PRO Query posts by repeater sub-field

Solved

Query posts by repeater sub-field

  • I’m trying to create a filter where a user can choose from select fields that will show related posts. I’m able to get the filter to work with the custom post taxonomies, but I also need to filter by custom post, repeater sub-field.

    I’ve read the forums and also https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
    and I’m still not able to get it to work. It’s still throwing no results when the select menu option is picked.

    
    <?php 
    function my_posts_where( $where ) {
    	$where = str_replace("meta_key = 'dist_product_variations_%", "meta_key LIKE 'dist_product_variations_%", $where);
    	return $where;
    }
    add_filter('posts_where', 'my_posts_where'); 
    
    $distribution_query = array(
    	'post_type' => 'distribution',
    	'posts_per_page' => 10,
    	'paged' => get_query_var('paged')
    );
    
    // filter by shape
    if(isset($_GET['shape']) && $_GET['shape'] != 'all') { 
    	$distribution_query['meta_query'] = array(
    		array(
    			'key'	  	=> 'dist_product_variations_%_dist_shape',
    			'value'	  	=> $_GET['shape'],
    			'compare' => 'LIKE',
    		)
    	);
    }
    
     //query
    $distribution_results = new WP_Query( $distribution_query );
    foreach($distribution_results->posts as $distribution) { 
    	echo $distribution->post_title;
    } wp_reset_postdata(); ?>
    
    

    When I change dist_product_variations_%_dist_shape to dist_product_variations_0_dist_shape or any other row number that has data, the filter works. But obviously all rows to work. I’ve also tried both LIKE and =, and no change.

    Please help!!!

  • Hi,

    this is a know problem in current WordPress Version 4.8.3.
    Please see: https://support.advancedcustomfields.com/forums/topic/percentage-in-sql/

    Pascal

  • Thank you!!!! This fixed it.

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

The topic ‘Query posts by repeater sub-field’ is closed to new replies.