1

Hello I am really sorry if this has been asked before but I couldn't find the exact solution I need. So here's the problem I want to create a search in my Wordpress page. Whenever a user submits the form I want to create a query to search for posts by their categories or their parents/child categories. Here's an example:

Example

So in MYSQL it would look something like this but I don't know how to do it in Wordpress with the help of WP_Query since I am new to this.

SELECT * FROM posts 
WHERE posts.category 
LIKE '%John Doe%' OR posts.sub_category LIKE '%John Doe%'

I haven't figured out how the tables are connected so probably a few JOIN operations need to be made here but I hope someone understands what I am asking.

Any help would be appreciated. Thanks in advance!

3
  • Probably easier to use the in-built methods for WP_Query available than learning complex SQL queries...
    – majick
    Feb 9, 2016 at 12:19
  • That's what I am asking, I can't figure out the WP_Query
    – Kanga
    Feb 9, 2016 at 12:39
  • Sorry the way you phrased it I read it like you wanted SQL.
    – majick
    Feb 10, 2016 at 3:02

2 Answers 2

0

Use get_categories first to get all the post categories to use in the form, then loop through them to display a multicheck input...

function hierarchical_category_inputs ($cat, $indent) {

    $cats = get_categories('hide_empty=false&orderby=name&order=ASC&parent='.$cat);
    $indent++;

    if ($cats) {
        foreach ($cats as $cat) {
           echo "<input type='checkbox' name='cat-".$cat->term_id."'";
           echo " style='margin-left: ".($indent*10)."px;'> ".$cat->name."<br>";
           hierarchical_category_inputs($cat->term_id, $indent);
        }
    }
}  

echo "<form method='post'>";
// 0 for all categories, -1 so first indent is 0
hierarchical_category_inputs(0,-1);
echo "<input type='hidden' name='custom_catsearch' value='yes'>";
echo "<input type='submit' value='Search'>";
echo "</form>";

Then in the search part you would assemble the array of ticked categories to pass to WP_Query:

function custom_catsearch_output() {
    $cats = array();
    foreach ($_POST as $key => $value) {
        // make sure the post key starts with cat-
        if (substr($key,0,4) == 'cat-') {
            // check for check of the checkbox
            if ($value == '1') {
                // get the cat id from after cat-
                $cats[] = substr($key,4,strlen($key));
            }
        }
    }

    $query = new WP_Query(array('cat' => $cats));

    print_r($query);
    // ... do something different with the results ...

}
2
  • Thanks for the detailed answer I will try something like this but was hoping for something easier like $query = WP_Query(array('cat_name' => 'Blackburn')); there's nothing similar to this?
    – Kanga
    Feb 10, 2016 at 12:41
  • you still need to use the cat ID but just get the term first: $term = get_term_by('name','Blackburn','category'); $query = new WP_Query(array('cat' => $term->term_id);
    – majick
    Feb 10, 2016 at 23:49
1

Use this:

$query = new WP_Query( array( 'cat' => 4 ) );

Where 4 is the ID of the top-level category (Blackburn). This query will in clude subcats.

Reference - WP_Query

2
  • But how do I find the id of the category I only know the name (e.g 'Blackburn') how do I know that's with the id of 4 or whatever?
    – Kanga
    Feb 9, 2016 at 23:31
  • This should probably be supplied by the form...
    – majick
    Feb 10, 2016 at 3:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.