Home » Wordpress » PHP/MySQL filtered search similar to Facebook

PHP/MySQL filtered search similar to Facebook

Posted by: admin November 30, 2017 Leave a comment

Questions:

I’m trying to build a simple custom search for a WordPress site using PHP and a mysql db that will return results from many different tables such as Groups, Users and Sites where none of the data per table relates to another table being searched. I’m aiming to build this similar to Facebook where it groups results based on the type of result.

I have the individual queries built but I am stuck on how to tie them all in together so that I can filter through the types of results and group them accordingly like Facebook does. Is there an easy way to tell which table results came from or would that be heading in the wrong direction entirely? I don’t know enough about mysql to know best practices. Here is a sample query that I am running:

SELECT 
    name, slug
FROM 
    ".$bp->groups->table_name."                              
WHERE 
    name LIKE '%".$search_string."%'
        OR 
    name = '%".$exp_string[0]."%'
        OR 
    name IN ('".$join_string."');
Answers:

Try this:

SELECT 
    name, slug, '$bp->groups->table_name' as table
FROM 
    ".$bp->groups->table_name."                              
WHERE 
    name LIKE '%".$search_string."%'
        OR 
    name = '%".$exp_string[0]."%'
        OR 
    name IN ('".$join_string."');

This should output the table name into the result. I don’t know how you may want to group it afterward..