我有一个包含以下字段的搜索表单:
这是我的wp_query
$args = array (
's' => (!empty($_REQUEST["search"])?$_REQUEST["search"]:''),
'post_type' => 'post',
'post_status' =>'publish',
'cat' => 5,
'posts_per_page' => 9,
'paged' => $paged,
'monthnum' =>(!empty($_GET["monthnum"])?$_GET["monthnum"]:''),
'year' => (!empty($_GET["year"])?$_GET["year"]:''),
'orderby' =>(!empty($_GET["orderby"])?$_GET["orderby"]:'date'),
'order' => (!empty($_GET["order"])?$_GET["order"]:'DSCE'),
);
当我选择月份和年份时,我会得到404 Not Found,并且URL的结尾看起来像这样
?search=&monthnum=10&year=2016
当我在wp_query中对月份和年份进行硬编码时,它将起作用并显示正确的帖子
那么当我通过URL传递月份和年份时为什么不起作用?
以及如何让用户按月份和年份搜索帖子?
答案 0 :(得分:0)
'monthnum'字段接受您不能将''传递给它的int值。试试这个代码-
$args = array (
's' => (!empty($_REQUEST["search"])?$_REQUEST["search"]:''),
'post_type' => 'post',
'post_status' =>'publish',
'cat' => 5,
'posts_per_page' => 9,
'paged' => $paged,
(!empty($_GET["monthnum"])?'monthnum' =>$_GET["monthnum"]:false),
(!empty($_GET["year"]))?'year' => $_GET["year"]:false),
'orderby' =>!empty($_GET["orderby"])?$_GET["orderby"]:'date',
);