Support

Account

Home Forums Front-end Issues Querying posts filtered by custom field values

Helping

Querying posts filtered by custom field values

  • Hi,

    I’m trying to query posts based on sub custom field values as in example 5 on
    http://www.advancedcustomfields.com/resources/how-to-query-posts-filtered-by-custom-field-values/.

    My CPT (named “trip”) has a repeater field called “departure_date”, which has a sub field called “departure_day”:

    function my_posts_where( $where ) {
    	$where = str_replace("meta_key = 'departure_date'", "meta_key LIKE 'departure_date'", $where);
    	return $where;
    }
    add_filter('posts_where', 'my_posts_where');
    $args = array(
    	'numberposts' => -1,
    	'post_type' => 'trip',
    	'meta_query' => array(
    		array(
    			'key' => 'departure_day',
    			'value' => 0,
    			'compare' => '>'
    		)
    	)
    );
    $get_trips_date = new WP_Query( $args );
    if( $get_trips_date->have_posts() ):
    	while ( $get_trips_date->have_posts() ) : $get_trips_date->the_post();
    		if( get_field('departure_date') ) {
    			while( has_sub_field('departure_day') ) { 
    				echo get_sub_field('departure_day');
    			}
    		}
    	endwhile;
    endif; 
    wp_reset_query();

    Although sub field “departure_day” is populated for all posts, this code returns nothing. Why?

  • I guess, the plugin Ultimate WP Query Search Filter can do the thing

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

You must be logged in to reply to this topic.