0

I am following the idea of Mayeenul Islam for creating a search for custom post types available here This link. However ,I want to use ACF repeater sub fields in the query.So,I have added another query for ACF Repeater field.. This is the code I have added to the search result template

// Get data from URL into variables
$departure_city_value = $_GET['departure-city'] != '' ? $_GET['departure-city'] : '';
$destination_city_value = $_GET['destination-city'] != '' ? $_GET['destination-city'] : '';
$journey_value = $_GET['onward-date'] != '' ? $_GET['onward-date'] : '';

// Start the Query
$v_args = array(
        'post_type'     =>  'event-main', // your CPT
        'meta_query'    =>  array(
                                array(
                                    'key'     => 'event_departure_city', // meta key
                                    'value'   => $departure_city_value,
                                    'compare' => 'LIKE', // compares
                                ),
                                 array(
                                    'key'     => 'event_destination_city', // meta key
                                    'value'   => $destination_city_value,
                                    'compare' => 'LIKE', // compares
                                ),

                            )
    );
$vehicleSearchQuery = new WP_Query( $v_args );

// Open this line to Debug what's query WP has just run
// var_dump($vehicleSearchQuery->request);


//Another Query for ACF Repeater Rows

$args = array(
    'post_type' => 'event-main',
    'posts_per_page' => 1
);
$row_id = get_the_ID();

$latestMessageQuery = new WP_Query($args);
if( $latestMessageQuery->have_posts() ) {
    while ($latestMessageQuery->have_posts()) : $latestMessageQuery->the_post();
        if( have_rows('event_instance') ):
            while( have_rows('event_instance') ): the_row();
                if( get_sub_field('event_bus_date') == $journey_date ):
                    the_sub_field('event_bus_date');

                    break;
                endif;
            endwhile;
        endif;
    endwhile;
}
wp_reset_query();

// Show the results
if( $vehicleSearchQuery->have_posts() ) :
    while( $vehicleSearchQuery->have_posts() ) : $vehicleSearchQuery->the_post();
        the_title(); // Assumed your cars' names are stored as a CPT post title

    endwhile;
else :
    _e( 'Sorry, nothing matched your search criteria', 'textdomain' );
endif;
wp_reset_postdata();
?>

Now the search result displays the title of the custom post. I also want to display ACF rows containing the selected sub field. Please help

  • I'm a bit confused on what results you are expecting to get - is the title and ACF rows coming from exactly the same post based on $_GET parameters? – Levi Dulstein Sep 1 '18 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.