• Hi!

    I just build a site for a filmfestival in Denmark. Movies are added to a program and a calendar, etc., with a lot af data attached, like director, original title, etc.

    But it seems like the WP search doesn’t support search in custom fields. I cannot find search for a movie’s original title.

    I’ve been Googling for hours now. I can’t find a solution… In my ears it actually sounds quite simple, maybe that’s why Google isn’t giving me anything.

    Any help is appreciated.

    Thanks.

Viewing 15 replies - 1 through 15 (of 15 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Did you try this plugin: wp-custom-fields-search
    I don’t know if it works with wordpress 3.0 but you could give it a try

    Thread Starter arnii

    (@arnii)

    It does work, but I cannot filter just one category with that plugin. Right now when I search a movie here; https://filmfestival.dk/en/category/programme/ the results get another layout than the default search, it only search one category, etc. These settings aren’t possible with the plugin…

    Moderator keesiemeijer

    (@keesiemeijer)

    In your search template you can try a query_posts with custom field data.
    something like this (put this before the loop):
    <?php query_posts('meta_key=movie&meta_value='.$s); ?>
    the $s is the global search string produced by WordPress.

    @arnii
    Wow very very nice movie db calendar setup you have there. Would you consider writing a tutorial someday on how you accomplished it?

    Thread Starter arnii

    (@arnii)

    @ keesiemeijer
    That could be a possibility. I’m going to try that and let you know…

    @anointed
    Thanks! It has been an exciting project –?and yeah, maybe I should write a tutorial on this. There are so many great technically things on this site.

    Thread Starter arnii

    (@arnii)

    @keesiemeijer
    That didn’t do anything… Actually I’m surprised that this isn’t included in the default search. Custom fields are often used for data which should be searchable.

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with only the meta value.
    <?php query_posts('meta_value='.$s); ?>
    Im going to check if I can make it work on my server. Why is the information like: movie title, director and so on not in the posts?

    Thread Starter arnii

    (@arnii)

    @keesiemeijer
    It actually to make it easier for the client. I’m using the plugin More Fields, to make custom fields for those data.

    Furthermore it’s much easier to work with the data that way in the templates.

    Moderator keesiemeijer

    (@keesiemeijer)

    This works for me:
    On search.php the loop was like this:

    <?php if ( have_posts() ) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <!-- the loop -->
    <?php endwhile; ?>
    <?php else : ?>
    <!-- sorry no posts -->
    <?php endif; ?>

    I changed it to:

    <?php query_posts('meta_value='.$s); ?>
    <?php if (!empty($wp_query->posts)) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <!-- the loop -->
    <?php endwhile; ?>
    <?php else : ?>
    <!-- sorry no posts -->
    <?php endif; ?>

    The normal search query thinks there are no posts so that’s why <?php if ( have_posts() ) : ?> cannot be used.

    @arnii
    Is there a good way to get in contact with you?
    I would absolutely love the opportunity to spend a few minutes chatting someday about what you are working on there.

    Thread Starter arnii

    (@arnii)

    @keesiemeijer
    The problem is, that when I use your code there, it breaks the default search.

    But anyway, thank you very much for your efforts. I don’t think it’s possible to do what I want.

    @anointed
    I answered your email yesterday ??

    Moderator keesiemeijer

    (@keesiemeijer)

    You could create your own search page to show your “film” search results. And the normal search.php for al the other search results
    or multiple loops. One for the normal search results and one for the meta search results:

    <?php if ( have_posts() ) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <!-- the first normal searh loop -->
    <?php endwhile; ?>
    <?php endif; ?>
    
    <?php $my_query = new WP_Query('meta_value='.$s); ?>
    <?php if (!empty($my_query->posts)) : ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <!-- the second meta_value searh loop -->
    <?php endwhile; ?>
    <?php endif; ?>

    @keesiemeijer Hi – having looked around for ages I came across your solution which was great and worked although I couldn’t get it quite right.

    I’m using a category as a staff list for a company. I have an A-Z display of thumbnails etc which are all custom fields including their first and last name {2 separate custom fields}. So by using your idea <?php query_posts('meta_value='.$s); ?> works great if people enter say a first or last name, but if someone types in both {to the search box}, no results are displayed as it’s now looking for a first and lastname.

    Any advice or help would be much appreciated.

    To query a bunch of custom fields I found it easier to use the search filters instead.

    https://codex.www.ads-software.com/Custom_Queries Scroll down to the “Keyword Search in Plugin Table” section for an example.

    Here’s a quick snippet of code form my custom ‘posts_where’ filter so you can get an idea:

    function custom_search_where($where) {
        // put the custom fields into an array
        $customs = array('custom_field1', 'custom_field2', 'custom_field3');
    
        foreach($customs as $custom) {
    	$query .= " OR (";
    	$query .= "(m.meta_key = '$custom')";
    	$query .= " AND (m.meta_value  LIKE '{$n}{$term}{$n}')";
            $query .= ")";
        }
    
        $where = " AND ({$query}) AND ($wpdb->posts.post_status = 'publish') ";
        return($where);
    }
    add_filter('posts_where', 'custom_search_where');
    

    There’s a lot more code but between the Codex example and the snippet above, it should give you a good idea.

    Thanks David, i’ll have a look. Is that code in effect grabbing your search terms (multiple meta fields) and building a query to search across all the terms?

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Include custom field values in search’ is closed to new replies.