Search CPT Title AND Meta

时间:2013-08-20 作者:Austin Biggs

我目前有一个CPT我想搜索,问题是我还需要能够搜索CPT的元数据。我有以下代码,目前搜索所需的元数据,但我似乎无法让它搜索标题和元数据。非常感谢您的帮助!

<?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(); ?>

1 个回复
SO网友:epilektric

看来你只是在用$search. 你还需要通过$searchs.

$args[\'s\'] = $search

这里有一个类似的问题。Search that will look in custom field, post title and post content

结束