Snippet : Change Search Posts mode to "contains"

This topic contains 1 reply, has 2 voices, and was last updated by  Alex Rollin 4 years, 1 month ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket

Tagged: 

  • Author
    Posts
  • #536302

    Alain CHAXEL
    Expired Member
    Post count: 18

    When you’re trying to link a post to another for example, the suggested results are post titles starting with your query string. You may want to get post titles containing your string.

    Here is a snippet to change SQL LIKE mode when querying posts via ajax in “Choose Place” dropdown

    
    
     
    /**
     * Change SQL LIKE mode when searching for a Place from String% to %String%
     * 
     * @param $where
     * @param $search
     * @param $post_type
     * @param $custom_field
     *
     * @return array|string
     */
    function my_prefix_cp_search_posts_query_where($where, $search, $post_type, $custom_field){
        global $wpdb;
        if ( $search != '' ) {
            $where = explode(' AND ', $where);
            foreach ($where as $key => $w){
                if(strpos($w, 'p.post_title LIKE') !== false){
                    unset($where[$key]);
                }
            }
            $where[] = $wpdb->prepare( "p.post_title LIKE %s", array( '%' . $search . '%' ) );
            $where = implode( " AND ", $where );
        }
        return $where;
    }
    add_filter( 'geodir_cp_search_posts_query_where', 'my_prefix_cp_search_posts_query_where', 10, 4 );
    
    #536329

    Alex Rollin
    Moderator
    Post count: 27815

    A valid license is required to view this reply.

    You may need to login

    OR

    Buy either a Membership or valid license for this product.

    Thanks, Team GeoDirectory!

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

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket