Custom search using WP Query, using custom fields and CPTS

I'd like to create a custom search (not just a custom form) as described in the second part of this article on wpmudev:

Sadly, this post doesn't have code I can model to create my own!

Let's say I have a custom post type Book, with a custom field Author, and custom taxonomy Genre.

I'd like a custom search form, limited to the Book CPT, where a visitor can choose to search for:

a term in the title or content of the Book (text search box), the author (text search box), and the genre (drop down select box).

If any of these is left blank, the search would ignore that aspect of the search. So if a person wanted to search for a Book with the word "ghost" in it of the genre "suspense," the author aspect of the search would be ignored, since the person didn't fill that field in.

Could you please tell me how to set up a WP Query object to perform this search, as well as a new search form and a search results page?

Many thanks!


  • Laura

    Thanks, Michael.

    At the very least, I'd appreciate some guidance as to how to set up a secondary WP Query search (in other words, leave the main site search alone and create a separate search) that combines searching for terms in title and content and a meta_query search. (I can set up the meta_query part myself.)

    I used the Book example to make my question clear. Those aren't my specifics.

    I training to be a WP developer myself, and I want to be able to do this. I definitely don't want to hire someone else for this project.



  • Hoang Ngo

    @ Laura,

    So for summary, this is what you have
    1. Book custom post type - _posts table
    2. Author - Custom field, WordPress has a native author field for post, so I think better you can use that?. Of course, if you don't want, you can still use custom field, which value will be stored in _postmeta table
    3. Custom Taxonomy Genre
    ====================== WP Query Part=================================
    Please check the code below, I has include some comments there so you can understand it

    $title = isset($_POST['book_title']) ? $_POST['book_title'] : null;
    $genre = isset($_POST['book_genre']) ? $_POST['book_genre'] : null;
    $book_author = isset($_POST['book_author']) ? $_POST['book_author'] : null;
    $args = array();
    //we check does user has submit title value
    if (!empty($title)) {
        //using s param, as we in searching scenario
        $args['s'] = $title;
    //now check does the genre has submited, because genre is custom taxonomy,
    //so we will use tax_query
    if (!empty($genre)) {
        $args['tax_query'] = array(
                //taxonomy name, in case this is genre
                'taxonomy' => 'genre',
                //we using slug
                'field' => 'slug',
                'terms' => $genre,
    //now the author, as it custom field, so we will look into the _postmeta table,
    //using meta_query
    if (!empty($book_author)) {
        $args['meta_query'] = array(
                //key is the thing you define for storing value, I assume this is author
                'key' => 'author',
                'value' => $book_author,
    //lastly, you are using custom post type, so you need to include that
    //assume this is "book"
    $args['post_type'] = 'book';
    //all set, now we just call the query
    $query = new WP_Query($args);

    You can check here for more information and example
    For form, this is a pretty basic form, you should check the tutorial here
    What's you need is text input and select
    After you clear how to build a html form, you can use this function, for output a dropdown

    $args = array(
        'hierarchical'       => 0,
        'name'               => 'book_genre',
        'taxonomy'           => 'genre',
        'hide_if_empty'      => false,

    ==============================RESULT PAGE======================
    This is somehow complex, and will require skill about php, fortunately, WordPress has a very good article so you can follow

    If you have any issues please don't hesitate to let us know so we can assist

    Best regards,

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.