按年份搜索自定义帖子类型


Search custom post type by year

我为标准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'])
            ),
        ),
    );
}

解决方法:

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