Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

When I search on my site it currently doesn't search the content in my custom meta boxes.

How can I include this content when searching?

I'm registering my meta boxes as follows:

$meta_box['recipe'] = array(
    'id' => 'recipe-meta-details',
    'title' => 'Recipe Meta Details',
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
         array(
            'name' => 'Country',
            'desc' => 'Country dish is from',
            'id' => 'recipe_country',
            'type' => 'text',
            'default' => ''
        ),
        array(
            'name' => 'Serves',
            'desc' => 'Number of people dish serves',
            'id' => 'recipe_serves',
            'type' => 'text',
            'default' => ''
        ),
         array(
            'name' => 'Ingredients',
            'desc' => '',
            'id' => 'recipe_ingredients',
            'type' => 'wysiwyg',
            'default' => ''
        ),
         array(
            'name' => 'Method',
            'desc' => '',
            'id' => 'recipe_method',
            'type' => 'wysiwyg',
            'default' => ''
        )
    )
);

And displaying them like this in my custom post archive loop:

$recipe_country = get_post_meta($post->ID, 'recipe_country', true);
$recipe_serves = get_post_meta($post->ID, 'recipe_serves', true);
$recipe_ingredients = get_post_meta($post->ID, 'recipe_ingredients', true);
$recipe_method = get_post_meta($post->ID, 'recipe_method', true);

<div id="post-content-left">
<h3>Ingredients</h3>
<?php echo $recipe_ingredients; ?>
</div>

<div id="post-content-right">
<h3>Method</h3>
<?php echo $recipe_method; ?>
</div>

Thanks

share|improve this question
    
It can't search it by default as it don't know the meta data key names. You name your custom fields (meta data) how you want. –  kaiser Jan 30 '12 at 8:47
    
Thanks Kaiser but I'm not quite sure what you mean. For example on this page here - bit.ly/xTcDJi - searching doesn't look at the text in the 'ingredients' or 'method' sections because that content is in custom meta boxes. –  fxfuture Jan 30 '12 at 9:33
    
That WordPress doesn' know stuff like 'ingredients' by default. How should it? You added it and it's custom. Therefore the name custom field and meta box. That's no error, but default behavior. So it can get fixed. Only added. The problem is that no one knows how excatly your meta data gets added. You showed no screenshots & no code. Without that it's only guessing. You have enough rep-points to know how you should ask a Q. –  kaiser Jan 30 '12 at 18:18
1  
My apologies Kaiser. I thought that this would probably be a straight forward answer and therefore no need to include any code. I've updated my question. Thank you –  fxfuture Jan 30 '12 at 18:45
    
Are you using the RW_Meta_Box class? –  kaiser Jan 30 '12 at 20:01
show 1 more comment

2 Answers

up vote 1 down vote accepted

try the Relevanssi plugin.

share|improve this answer
    
thanks. i'll take a look at the plugin –  fxfuture Jan 30 '12 at 18:47
    
This plugin is amazing - just what i needed and more! Thank you –  fxfuture Jan 30 '12 at 21:40
add comment

I had this problem recently. I ended up using WP_Query to amend my site's search results.

I've abridged some of the code, but essentially this is what I did. Edit your theme's search.php.

First off, I create an array of arguments called "meta_query". You can also do this for "tax_query" if you have custom taxonomies attached to your Custom Post Type. You'll likely need more than one of these if you have more than one field in your meta box(es).

<?php
    $aquarium_H = array(
        'key' => 'aquarium_H', //name of your meta field
        'value' => $_GET["s"], // value from WordPress search bar. this is sanitized by WP
        'type' => 'numeric', // string/numeric/whatever
        'compare' => '<=' // this can be "LIKE" or "NOT LIKE". most mySQL operators
    );

    $meta_query[] = $aquarium_H; // add to meta_query array

Next I check to see if this array exists and if it does I put it as the meta_query value of an $args array.

    if (isset($meta_query)) {
        $args = array(
            'post_type' => 'species', // can be "any"
            'meta_query' => $meta_query
        );
    }
?>

This means that when I use the following line, the search value (derived from $_GET["s"], which is the default name of the WordPress search box) is looked for in the meta boxes you've specified.

<?php $query = new WP_Query ( $args ); ?>

You then need to adapt the code on your theme somewhat. At this point, I don't think "just" have_posts will work - you need to specify the $query object.

<?php if ($query->have_posts()) : ?>

    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
        <div class="post" id="post-<?php the_ID(); ?>">
            <?php /* the_content could go here. I use different code */ ?>
        </div>
    <?php endwhile; ?>

<?php else : ?>

Apologies if this is a bit confusing.. !

share|improve this answer
1  
If you're doing a new WP_Query; after wp() was called and the query was built, then you should reset the posts first. Anyway +1 But you should search for "wp_query() vs. get_posts()" here on WPSE. Rarst has written a good tutorial about this topic. –  kaiser Jan 30 '12 at 18:22
    
Will do, many thanks :) –  dunc Jan 30 '12 at 18:41
    
@dunc - thank you. I'll give this a go –  fxfuture Jan 30 '12 at 18:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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