Support

Account

Forum Replies Created

  • function cf_search_join( $join ) {
        global $wpdb;
    
        if ( is_search() ) {    
            $join .=' LEFT JOIN '.$wpdb->postmeta. ' cfmeta ON '. $wpdb->posts . '.ID = cfmeta.post_id ';
        }
    
        return $join;
    }
    add_filter('posts_join', 'cf_search_join' );
    
    function cf_search_where( $where ) {
        global $pagenow, $wpdb;
    
        if ( is_search() ) {
            $where = preg_replace(
                "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
                "(".$wpdb->posts.".post_title LIKE $1) OR (cfmeta.meta_value LIKE $1)", $where );
        }
    
        return $where;
    }
    add_filter( 'posts_where', 'cf_search_where' );
    
    function cf_search_distinct( $where ) {
        global $wpdb;
    
        if ( is_search() ) {
            return "DISTINCT";
        }
    
        return $where;
    }
    add_filter( 'posts_distinct', 'cf_search_distinct' );
  • (I have problems to insert code, I have commented like 4 times hahaha)
    Sorry. My English is bad. That code works only to search by custom fields.

    If you want to add custom fields to the search, then you need to use this.

  • Sorry. My English is bad. That code works only to search by custom fields.

    If you want to add custom fields to the search, then you need to use this.

    
    function cf_search_join( $join ) {
        global $wpdb;
    
        if ( is_search() ) {    
            $join .=' LEFT JOIN '.$wpdb->postmeta. ' cfmeta ON '. $wpdb->posts . '.ID = cfmeta.post_id ';
        }
    
        return $join;
    }
    add_filter('posts_join', 'cf_search_join' );
    
    function cf_search_where( $where ) {
        global $pagenow, $wpdb;
    
        if ( is_search() ) {
            $where = preg_replace(
                "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
                "(".$wpdb->posts.".post_title LIKE $1) OR (cfmeta.meta_value LIKE $1)", $where );
        }
    
        return $where;
    }
    add_filter( 'posts_where', 'cf_search_where' );
    
    function cf_search_distinct( $where ) {
        global $wpdb;
    
        if ( is_search() ) {
            return "DISTINCT";
        }
    
        return $where;
    }
    add_filter( 'posts_distinct', 'cf_search_distinct' );
    
  • Sorry. My English is bad. That code works only to search by custom fields.

    If you want to add custom fields to the search, then you need to use this.

    function cf_search_join( $join ) {
        global $wpdb;
    
        if ( is_search() ) {    
            $join .=' LEFT JOIN '.$wpdb->postmeta. ' cfmeta ON '. $wpdb->posts . '.ID = cfmeta.post_id ';
        }
    
        return $join;
    }
    add_filter('posts_join', 'cf_search_join' );
    
    function cf_search_where( $where ) {
        global $pagenow, $wpdb;
    
        if ( is_search() ) {
            $where = preg_replace(
                "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
                "(".$wpdb->posts.".post_title LIKE $1) OR (cfmeta.meta_value LIKE $1)", $where );
        }
    
        return $where;
    }
    add_filter( 'posts_where', 'cf_search_where' );
    
    function cf_search_distinct( $where ) {
        global $wpdb;
    
        if ( is_search() ) {
            return "DISTINCT";
        }

    return $where;
    }
    add_filter( ‘posts_distinct’, ‘cf_search_distinct’ );
    `

  • Sorry. My English is bad. That code works only to search by custom fields.

    If you want to add custom fields to the search, then you need to use this.

    function cf_search_join( $join ) {
        global $wpdb;
    
        if ( is_search() ) {    
            $join .=' LEFT JOIN '.$wpdb->postmeta. ' cfmeta ON '. $wpdb->posts . '.ID = cfmeta.post_id ';
        }
    
        return $join;
    }
    add_filter('posts_join', 'cf_search_join' );
    
    function cf_search_where( $where ) {
        global $pagenow, $wpdb;
    
        if ( is_search() ) {
            $where = preg_replace(
                "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
                "(".$wpdb->posts.".post_title LIKE $1) OR (cfmeta.meta_value LIKE $1)", $where );
        }
    
        return $where;
    }
    add_filter( 'posts_where', 'cf_search_where' );
    
    function cf_search_distinct( $where ) {
        global $wpdb;
    
        if ( is_search() ) {
            return "DISTINCT";
        }
    
        return $where;
    }
    add_filter( 'posts_distinct', 'cf_search_distinct' );
  • it works for me

    function custom_search_query( $query ) {
        if ( !is_admin() && $query->is_search ) {
    		$result = $query->query_vars['s'];
    		$query->query_vars['s'] = '';
            $query->set('meta_query', array('relation' => 'OR',
                array(
    				'key'     => 'acf_name', // ACF FIELD NAME OR POST META
    				'value'   => $result,
    				'compare' => 'LIKE',
                )
            ));
            $query->set('post_type', 'servicios'); // optional POST TYPE
        }
    }
    add_filter( 'pre_get_posts', 'custom_search_query');
    
Viewing 6 posts - 1 through 6 (of 6 total)