是否搜索附加了所有元的自定义帖子类型?

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

我现在有一个搜索框,用来搜索特定的帖子类型。它应该根据所有标准cpt内容、标记和相关元数据检查查询。我想知道如何修改代码,以便在CPT中搜索与帖子相关的所有数据。

以下是我当前拥有的内容(当前仅搜索“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 个回复
SO网友:Jacob Raccuia

我尝试了这个查询,它似乎运行得很好。希望它也能帮助你!

$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 );

结束

相关推荐

Paginate tags page

如果我单击一个随机标记,我希望标记页面只列出20篇与该标记相关的文章并分页。<?php /** * The template for displaying Tag Archive pages. */ get_header(); ?> <div id=\"container\"> <div id=\"content\" role=\"main\"> &#