3

I'm currently have a search box that's supposed to search a certain post type. It's supposed to check the query against all standard cpt content, tags and the associated meta data. I'm wondering how I would modify my code to allow searching all of the data associated with a post in a CPT.

Here's what I currently have (currently only searches 'memberFirstName'):

<?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' => 'cbd_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(); ?>
1

I tried this query and it seemed to work quite nicely. Hopefully it helps you as well!

$args = array(
    'post_type' => 'cbd_team_members', 
    'posts_per_page' => -1,
    'paged' => $paged, 
    'orderby' => 'title', 
    'order' => 'ASC', 
    'meta_value' => $search,
    'meta_compare' => "like"
);
$loop = new WP_Query( $args );

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.