Home Forums General Issues Searching with ACF and the_title()


Searching with ACF and the_title()

  • Hi there,

    I have the following code to search custom ACF fields which works fine:-

    // array of filters (field key => field name)
    $GLOBALS['my_query_filters'] = array(
    'field_1' => 'job_sector',
    'field_2' => 'job_type',
    'field_3' => 'job_location',
    'field_4' => 'job_salary_from',
    'field_5' => 'job_salary_to',
    // action
    add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);
    function my_pre_get_posts( $query ) {
    // bail early if is in admin
    if( is_admin() ) {
    // get meta query
    $meta_query = $query->get('meta_query');
    // loop over filters
    foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
    // continue if not found in url
    if( empty($_GET[ $name ]) ) {
    // get the value for this filter
    // eg:,sydney
    $value = explode(',', $_GET[ $name ]);
    // append meta query
         $meta_query[] = array(
                'key' => $name,
                'value' => $value,
                'compare' => 'IN',
    // update meta query
    $query->set('meta_query', $meta_query);

    However, within this search I also need to search ‘the_title’, how is it possible to do this?

  • Hi @nsilva

    I believe you can get the post based on the keyword using the “s” parameter. This page should give you more idea about it: Something like this:

    // update meta query
    $query->set('s', 'the keyword');
    $query->set('meta_query', $meta_query);

    Hope this helps.

  • When I use the above, and for example search the following:-

    It now loads a different page template, it needs to search the archive only, I don’t know if this makes it any different.

    Usually the template loads as follows:-

  • Hi @nsilva

    The ?s= parameter is used by WordPress’ search function so you will get redirected to the search page when you are accessing that URL. You need to use another parameter like “search”, for example, so your link will look like this:$search=thekeyword

    For the code, you can use something like this:

    // update meta query
    if( !empty($_GET[ 'search' ]) ) {
        $query->set('s', $_GET[ 'search' ]);
    // update meta query
    $query->set('meta_query', $meta_query);

    I hope this helps.

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

The topic ‘Searching with ACF and the_title()’ is closed to new replies.