按年搜索自定义帖子类型

时间:2013-11-18 14:11:51

标签: php wordpress

我为标准的Wordpress搜索创建了更高级的搜索。为了配合关键字文本输入,我添加了两个用于搜索自定义分类的下拉菜单。

这很有效。

我尚未创建功能的唯一下拉菜单是年份下拉菜单。我希望能够过滤结果以显示特定年份内的帖子。

如何搜索所有帖子并仅输出当年发布的自定义帖子?

通过发布日期搜索帖子非常重要(即没有自定义字段!)。

<form action="<?php echo home_url( '/' ); ?>" method="get" id="advanced-search">
    <div id="search-nudge">
        <h1 id="search-title">Search Policy's</h1>
        <p id="search-strap">Search by some or all of the following criteria</p>
        <div class="clear"></div>

        <div id="search-left">
            <p class="search-label">Keyword</p>
            <input type="text" name="s" id="s" class="keyword" value="<?php the_search_query(); ?>" />
            <input type="hidden" value="policies" name="post_type" id="post_type" /><br /><br />
        </div>

        <div id="search-right">
            <p class="search-label">Year of publication</p>
            <select class="work-list search-dropdown" name="year">
                <option value="">All</option>
            </select>
            <div class="clear"></div>                   
        </div>
        <div class="clear"></div>

        <div class="search-element">                    
            <?php // List all event subjects
            $subjectList = get_terms('topics', array( 'taxonomy' => 'topics' ));
            if(!empty($subjectList)) { ?>
                <p class="search-label">Category</p>
                <select class="work-list search-dropdown" name="subject">
                <option value="">All</option>
            <?php foreach($subjectList as $subject){
                    if(isset($_GET['subject'])) {
                        $selected = ($subject->slug == $_GET['subject']) ? ' selected="selected"' : '';
                    }
                    echo '<option value="' . $subject->slug . '"'. $selected.'> ' . $subject->name . '</option>';
                }
                echo '</select><br /><br />';
            } ?>
        </div>

        <div class="search-element">                    
            <?php // list all event types
            $document = get_terms('document_type', array( 'taxonomy' => 'document_type' )); 
            if(!empty($subjectList)) { ?>
                <p class="search-label">Document Type</p>
                <select class="work-list search-dropdown search-dropdown-right" name="type">
                <option value="">All</option>                   
            <?php foreach($document as $type){
                if(isset($_GET['type'])) {
                        $selected = ($type->slug == $_GET['type']) ? ' selected="selected"' : '';
                    }
                    echo '<option value="' . $type->slug . '"'. $selected.'> ' . $type->name . '</option>';
                }
                echo '</select><br /><br />';
            } ?>
        </div>

        <input type="radio" name="sort" class="search-input" value="date" checked> <p class="search-label radio radio-nudge">Order by date</p>
        <input type="radio" name="sort" class="search-input" value="title"> <p class="search-label radio">Order by title</p>

        <input type="submit" id="searchsubmit2" class="search-submit" value="Search" />
        <div class="clear"></div>
    </div>
</form>
<?php if(isset($_GET['sort']) && $_GET['sort'] === "date") {
    $sort = $_GET['sort'];
    $order = "DESC";
} else {
    $sort = $_GET['sort'];    
    $order = "ASC";
}               

// default args to search by custom post type and search term
$args = array(
    'post_type' => 'policies',
    'posts_per_page'   => 10,
    'orderby' => $sort,
    'order' => $order,
    's'    => $s
);      

// add the subject to args if there is one
if(isset($_GET['subject']) && $_GET['subject'] != "") {
    $args['tax_query'][1] = array(
        'taxonomy' => 'topics',
        'field' => 'slug',
        'terms' => array($_GET['subject'])
    );
}   

// add the subject to args if there is one
if(isset($_GET['type']) && $_GET['type'] != "") {
    $args['tax_query'][1] = array(
        'taxonomy' => 'document_type',
        'field' => 'slug',
        'terms' => array($_GET['type'])
    );
}                               

// Run the query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        the_title();
        the_content();
    }
} else {
    echo "No results";
}

/* Restore original Post Data */
wp_reset_postdata(); ?>

更新

我尝试了以下但没有运气显示任何帖子。

if(isset($_GET['year']) && $_GET['year'] != "") {
    $args = array(
        'date_query' => array(
            array(
                'year'  => array($_GET['year'])
            ),
        ),
    );
}

1 个答案:

答案 0 :(得分:0)

解决:

        if(isset($_GET['year']) && $_GET['year'] != "") {
        $args = array(
            'post_type' => 'policies',
            'posts_per_page'   => 10,
            'orderby' => $sort,
            'order' => $order,
            's'    => $s,           
            'date_query' => array(
                    array(
                        'year'  => ($_GET['year'])
                    ),
                ),                          
        );
        } else {
            // default args to search by custom post type and search term
            $args = array(
                'post_type' => 'policies',
                'posts_per_page'   => 10,
                'orderby' => $sort,
                'order' => $order,
                's'    => $s
            );              
        }