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.

I currently have a CPT I would like to search, the catch is I also need to be able to search the meta data for the CPT. I have the following code that currently searches the meta value needed but I can't seem to get it to search the title AND the meta data. Help would be greatly appreciated!

<?php
/**
 * @package WordPress
 * @subpackage themename
 */

get_header(); 
$search = get_search_query(); ?>
<div id="main">
        <div id="primary" role="region">
            <div id="content">

                    <h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'themename' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
                        <?php
                            // LOOP ARGUMENTS
                            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                            $args = array( 'post_type' => 'team_members', 'posts_per_page' => -1, 'paged' => $paged, 'orderby' => 'title', 'order' => 'ASC', 'meta_query' => array(
                                    array(
                                        'key' => 'memberFirstName',
                                        'value' => $search,
                                        'compare' => 'LIKE'
                                        ),
                                    )); // -1 Shows ALL Posts
                            $loop = new WP_Query( $args );
                            while ( $loop->have_posts() ) : $loop->the_post();

                            // CUSTOM CONTENT
                            $memberFirstName = get_post_meta($post->ID,"memberFirstName",true);
                            $memberPosition = get_post_meta($post->ID,"memberPosition",true);
                            $memberPhone = get_post_meta($post->ID,"memberPhone",true);
                            $memberEmail = get_post_meta($post->ID,"memberEmail",true);
                            $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail_name');
                            $imgURL = (isset($thumb[0]) ? $thumb[0] : get_template_directory_uri() . "/images/member_placeholder.jpg");

                            $postID = get_the_ID();
                            $terms = get_the_terms( $postID, 'service-type' );
                            foreach($terms as $term) {
                                if($term->parent == 14 ) {
                                    $memberDivision = $term->name;
                                }
                                if($term->parent == 8 ) {
                                    $memberDivision = $term->name;
                                }
                                if($term->parent == 3 ) {
                                    $memberCity = $term->name;
                                }
                            }
                        ?>

                            <div class="memberListing">
                                <img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo urlencode($imgURL); ?>&h=79&w=79&zc=2">
                                <div class="memberInfo">

                                    <strong><?php echo $memberFirstName; ?>&nbsp;<?php the_title(); ?></strong>, <span><?php echo $memberPosition; ?></span><br />
                                    <?php echo $memberDivision; ?>, <?php echo $memberCity; ?>
                                    <div class="right">
                                        <?php echo $memberPhone; ?>
                                        <a href="mailto:<?php echo $memberEmail; ?>">Email</a>
                                    </div>
                                </div>
                            </div>          

                        <?php /* END WHILE AND RESET QUERY */ endwhile; wp_reset_query(); ?>
                        <div class="pagination">
                        <?php $total_pages = $loop->max_num_pages;

                            if ($total_pages > 1){

                                $current_page = max(1, get_query_var('paged'));

                                echo paginate_links(array(
                                'base' => get_pagenum_link(1) . '%_%',
                                'format' => '/page/%#%',
                                'current' => $current_page,
                                'total' => $total_pages,
                            ));
                        }   ?>
                        </div>
            </div><!-- #content -->

            <div id="secondary" class="widget-area">
                <?php if ( dynamic_sidebar('team-sidebar') ) : else : ?><?php endif; ?>
            </div>
        </div><!-- #primary -->


<?php get_footer(); ?>
share|improve this question
add comment

1 Answer

It seems you are only searching the meta with $search. You need to also pass $search to s.

$args['s'] = $search

Here's a similar question. Search that will look in custom field, post title and post content

share|improve this answer
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.