Roots Discourse

Parameter 2 to App\advanced_custom_search() expected to be a reference, value given


I am trying to implement a wider search in ACF fields. found a potential candidate here:

but, when I try to implement this in the setup.php page I receive the error in the object…

use wpdb;
use WP_Query;

function list_searcheable_acf(){
    $list_searcheable_acf = array("title", "operations_short_bio", "operations_long_bio");
    return $list_searcheable_acf;
 * [advanced_custom_search search that encompasses ACF/advanced custom fields and taxonomies and split expression before request]
 * @param  [query-part/string]      $where    [the initial "where" part of the search query]
 * @param  [object]                 $wp_query []
 * @return [query-part/string]      $where    [the "where" part of the search query as we customized]
 * see
 * credits to Vincent Zurczak for the base query structure/spliting tags section
function advanced_custom_search( $where, &$wp_query ) {
    global $wpdb;

    if ( empty( $where ))
        return $where;

    // get search expression
    $terms = $wp_query->query_vars[ 's' ];

    // explode search expression to get search terms
    $exploded = explode( ' ', $terms );
    if( $exploded === FALSE || count( $exploded ) == 0 )
        $exploded = array( 0 => $terms );

    // reset search in order to rebuilt it as we whish
    $where = '';

    // get searcheable_acf, a list of advanced custom fields you want to search content in
    $list_searcheable_acf = list_searcheable_acf();
    foreach( $exploded as $tag ) :
        $where .= "
          AND (
            (assoc_posts.post_title LIKE '%$tag%')
            OR (assoc_posts.post_content LIKE '%$tag%')
            OR EXISTS (
              SELECT * FROM assoc_postmeta
	              WHERE post_id = assoc_posts.ID
	                AND (";
        foreach ($list_searcheable_acf as $searcheable_acf) :
            if ($searcheable_acf == $list_searcheable_acf[0]):
                $where .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
            else :
                $where .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
        $where .= ")
            OR EXISTS (
              SELECT * FROM assoc_comments
              WHERE comment_post_ID = assoc_posts.ID
                AND comment_content LIKE '%$tag%'
            OR EXISTS (
              SELECT * FROM assoc_terms
              INNER JOIN assoc_term_taxonomy
                ON assoc_term_taxonomy.term_id = assoc_terms.term_id
              INNER JOIN assoc_term_relationships
                ON assoc_term_relationships.term_taxonomy_id = assoc_term_taxonomy.term_taxonomy_id
              WHERE (
          		taxonomy = 'post_tag'
            		OR taxonomy = 'category'
            		OR taxonomy = 'myCustomTax'
              	AND object_id = assoc_posts.ID
              	AND LIKE '%$tag%'
    return $where;

add_filter( 'posts_search', __NAMESPACE__.'\\advanced_custom_search', 500, 2 );

any hint on how to solve this?

Well, you are using & in the parameter of your function: function advanced_custom_search( $where, &$wp_query ) {

That function is registered as a hook for the posts_search filter.
WordPress core is invoking that function and passing the arguments as values, not as references, hence the PHP Warning.

and how can I solve this?

I may miss something, but as far as I can see, there doesnt seem to be any reason to pass $wp_query by reference since there is no alteration of it within the function.

So try removing the &-sign from the second argument passed to advanced_custom_search()

You can read about passing arguments by reference here:

If you are looking to improve the search functionality you may want to take a look at which can handle ACF and much more.

ty everyone.
I just copied the hook to test wether it was working or not.
the error disappeared but no results are no longer shown :sweat_smile:

so it’s just a partial victory :smiley:

No results probably means that no records match the query. So you have to debug the query.