我正在使用此代码归档一些自定义帖子类型:
$where .= " AND wp_postmeta.meta_value >= now()";
元值是一个日期字段。在datefrom值设置完毕的当天,post将获得存档。
我想它被存储为存档后的元日期1天。
有什么办法改变吗
$where .= " AND wp_postmeta.meta_value >= now()";
而不是现在?如果是,怎么可能?
以下是完整的功能:
function custom_posts_where( $where, $query ) {
if (is_singular()) return $where;
if (($query->query_vars[\'post_type\'] == \'events\' || (is_array($query->query_vars[\'post_type\']) && $query->query_vars[\'post_type\'][0] == \'events\'))) {
$year = 0;
$month = 0;
if (preg_match(\'@/([0-9]{4})/([0-9]{1,2})/@\', $_SERVER[\'HTTP_REFERER\'], $refer_match)) {
$year = $refer_match[1];
$month = $refer_match[2];
}
else if (preg_match(\'@/([0-9]{4})//@\', $_SERVER[\'HTTP_REFERER\'], $refer_match)) {
$year = $refer_match[1];
}
if (is_archive()) {
if (!is_month() && !is_year()) {
$where .= " AND wp_postmeta.meta_value >= now()";
}
else {
$where = preg_replace("@AND.*?\\) \\)@i", \'\', $where);
if (is_month() || is_year()) {
$where .= " AND YEAR(wp_postmeta.meta_value) = " . $query->query_vars[\'year\'];
}
if (is_month()) {
$where .= " AND MONTH(wp_postmeta.meta_value) = " . $query->query_vars[\'monthnum\'];
}
}
}
--编辑--
以下是从元值归档帖子的全套功能:
/* ############################################################################### */
/* ########## START: these are the problem functions ############# */
function custom_posts_orderby( $orderby, $query ) {
if (is_singular()) return $orderby;
if (($query->query_vars[\'post_type\'] == \'events\' || (is_array($query->query_vars[\'post_type\']) && $query->query_vars[\'post_type\'][0] == \'events\')) && !is_month()) {
if (is_archive()) {
if (!isset($query->query_vars[\'orderby\']) || $query->query_vars[\'orderby\'] == \'_simple_fields_fieldGroupID_1_fieldID_5_numInSet_0\') {
$orderby = "wp_postmeta.meta_value ASC";
}
}
if (stripos($orderby, \'post_title\') !== false) {
$orderbys = explode(" ", $orderby);
$orderbys[0] = "trim(" . $orderbys[0] . ")";
$orderby = implode(" ", $orderbys);
}
}
$year = 0;
$month = 0;
if (preg_match(\'@/([0-9]{4})/([0-9]{1,2})/@\', $_SERVER[\'REQUEST_URI\'], $refer_match)) {
$year = $refer_match[1];
$month = $refer_match[2];
}
else if (preg_match(\'@/([0-9]{4})//@\', $_SERVER[\'REQUEST_URI\'], $refer_match)) {
$year = $refer_match[1];
}
if ($year > 0 || $month > 0) {
$orderby = "wp_postmeta.meta_value ASC";
}
return $orderby;
}
function custom_posts_join( $join ) {
global $wp_query, $wpdb;
if (is_singular()) return $join;
if (($wp_query->query_vars[\'post_type\'] == \'events\' || (is_array($wp_query->query_vars[\'post_type\']) && $wp_query->query_vars[\'post_type\'][0] == \'events\')) && !is_month() && stripos($join, \'postmeta\') === false) {
if (is_archive()) {
$join .= " INNER JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id";
}
}
return $join;
}
function custom_parse_query( $query ) {
if ($query->query_vars[\'post_type\'] == \'events\' || (is_array($query->query_vars[\'post_type\']) && $query->query_vars[\'post_type\'][0] == \'events\')) {
if (is_archive()) {
$query->query_vars[\'meta_key\'] = \'_simple_fields_fieldGroupID_1_fieldID_5_numInSet_0\';
$query->query[\'meta_key\'] = \'_simple_fields_fieldGroupID_1_fieldID_5_numInSet_0\';
}
}
}
function custom_posts_where( $where, $query ) {
if (is_singular()) return $where;
if (($query->query_vars[\'post_type\'] == \'events\' || (is_array($query->query_vars[\'post_type\']) && $query->query_vars[\'post_type\'][0] == \'events\'))) {
$year = 0;
$month = 0;
if (preg_match(\'@/([0-9]{4})/([0-9]{1,2})/@\', $_SERVER[\'HTTP_REFERER\'], $refer_match)) {
$year = $refer_match[1];
$month = $refer_match[2];
}
else if (preg_match(\'@/([0-9]{4})//@\', $_SERVER[\'HTTP_REFERER\'], $refer_match)) {
$year = $refer_match[1];
}
if (is_archive()) {
if (!is_month() && !is_year()) {
$where .= " AND wp_postmeta.meta_value >= now()";
}
else {
$where = preg_replace("@AND.*?\\) \\)@i", \'\', $where);
if (is_month() || is_year()) {
$where .= " AND YEAR(wp_postmeta.meta_value) = " . $query->query_vars[\'year\'];
}
if (is_month()) {
$where .= " AND MONTH(wp_postmeta.meta_value) = " . $query->query_vars[\'monthnum\'];
}
}
}
else {
if (!is_search || is_home() ) {
$where .= " AND wp_postmeta.meta_value >= now()";
}
#} else {
#$where .= " AND (wp_postmeta.meta_value = \'_simple_fields_fieldGroupID_1_fieldID_5_numInSet_0\' AND wp_postmeta.meta_value >= now())";
#meta_key \'_simple_fields_fieldGroupID_1_fieldID_5_numInSet_0\'
#meta_value \'2013-10-28\'
#}
}
}
return $where;
}
function custom_posts_exclude( $query ) {
# do we need to put some stuff here?
}
# only affect the front-end pages only, not the admin
if ((!is_admin() || ( isset($_REQUEST[\'action\']) && $_REQUEST[\'action\'] == \'awpqsf_ajax\'))) {
//if (!$_GET[\'post_type_index\']) {
add_filter(\'posts_orderby\', \'custom_posts_orderby\', 10, 2);
add_filter(\'posts_join\', \'custom_posts_join\');
add_filter(\'parse_query\', \'custom_parse_query\');
add_filter(\'posts_where\', \'custom_posts_where\', 10, 2);
add_action(\'pre_get_posts\', \'custom_posts_exclude\' );
//}
}