Support

Account

Home Forums ACF PRO Query only against post_title in acf/fields/post_object/query Reply To: Query only against post_title in acf/fields/post_object/query

  • Apologies if this is a duplicate reply. The system hasn’t accepted previous submits.
    __

    I instead chose to run the __search_by_title_only function only when a user is logged in, which satisfies my requirement.

    Here’s the exact code I used:

    function __search_by_title_only( $search, $wp_query )
    {
    global $wpdb;

    $q = $wp_query->query_vars;
    $n = ! empty( $q[‘exact’] ) ? ” : ‘%’;
    $search =
    $searchand = ”;
    foreach ( (array) $q[‘search_terms’] as $term ) {
    $term = esc_sql( like_escape( $term ) );
    $search .= “{$searchand}($wpdb->posts.post_title LIKE ‘{$n}{$term}{$n}’)”;
    $searchand = ‘ AND ‘;
    }
    if ( ! empty( $search ) ) {
    $search = ” AND ({$search}) “;
    }
    return $search;

    }

    // call this function ONLY if user is logged in (e.g. admin, editor)
    if ( is_user_logged_in() ) {
    add_filter(‘posts_search’, ‘__search_by_title_only’, 500, 2);
    }

    FYI:
    I omitted the “&” from the 2nd parameter because it was throwing errors sitewide: “Parameter 2 to __search_by_title_only( ) expected to be a reference”

    Produced errors:
    function __search_by_title_only( $search, &$wp_query )

    Working:
    function __search_by_title_only( $search, $wp_query )