WordPress date_query 行为异常
Wordpress date_query behaving strangely
这里有点菜鸟。
我正在尝试编写一些代码来确定当前用户是否有:一个超过 30 天的帐户和 0 个 woocommerce 订单。
我有这个顶部部分在过去 90 天内获取属于用户的所有订单。
然而,它 returns 0 个订单,即使我的测试用户有 1 个已完成的订单。
我有预感可能是这一行:
'before' => 日期('Y-m-d', strtotime('now'))
所以我删除了它,然后它返回了“19 个订单”,这简直是不可能的。
print_r 部分好像只显示了 1 个订单的乱码元数据,所以它从哪里得到 19,我无法理解!
非常感谢对此问题的任何帮助!所有回声和 print_r 都用于调试目的。
function woo_reg_matured( $days_old = 30 )
{
$cu = wp_get_current_user();
return ( isset( $cu->data->user_registered ) && strtotime( $cu->data->user_registered ) < strtotime( sprintf( '-%d days', $days_old ) ) ) ? TRUE : FALSE;
}
function woo_inactive_shortcode( $atts = array(), $content = '' ) {
if ( is_user_logged_in() ):
$customer_orders = get_posts( array(
'numberposts' => - 1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => array( 'shop_order' ),
'post_status' => array( 'wc-completed' ),
'date_query' => array(
'after' => date('Y-m-d', strtotime('-90 days'))
) ) );
$total = 0;
print_r($customer_orders);
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order );
$total += $order->get_total();
}
echo 'Orders: ' . $total . '<br>';
if ($total <= 5) {
if( woo_reg_matured( $days_old = 30 ) ) {
echo 'You are inactive with ' . $total . ' orders and account age over 30 days';
return;
}
echo 'You are inactive with ' . $total . ' orders and account age under 30 days';
return;
}
echo 'You are active with over 30 days age and ' . $total . ' orders';
return;
endif;
}
add_shortcode( 'woo_inactive', 'woo_inactive_shortcode' );
print_r 显示:
Array ( [0] => WP_Post Object ( [ID] => 75675 [post_author] => 1 [post_date] => 2018-01-17 12:16:28 [post_date_gmt] => 2018-01-17 12:16:28 [post_content] => [post_title] => Order – January 17, 2018 @ 12:16 PM [post_excerpt] => [post_status] => wc-completed [comment_status] => open [ping_status] => closed [post_password] => order_5a5f3e9c442d6 [post_name] => order-jan-17-2018-1216-pm [to_ping] => [pinged] => [post_modified] => 2018-01-17 12:17:24 [post_modified_gmt] => 2018-01-17 12:17:24 [post_content_filtered] => [post_parent] => 0 [guid] => https://www.sheersense.com/?post_type=shop_order&p=75675 [menu_order] => 0 [post_type] => shop_order [post_mime_type] => [comment_count] => 3 [filter] => raw ) )
然而 $total
显示:Orders: 19
哪里来的19!!!!哈哈
这样试试。
if ( is_user_logged_in() ):
$customer_orders = get_posts( array(
'numberposts' => - 1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => array( 'shop_order' ),
'post_status' => array( 'wc-completed' ),
'date_query' =>
array('after' =>
array(
'year' => 2017,
'month' => 03,
'day' => 12,
),
'before' =>
array(
'year' => 2018,
'month' => 01,
'day' => 17,
),
) ) );
$total = 0;
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order );
print_r($customer_order);
//$total++;
$total += $order->get_total();
}
echo 'Orders: ' . $total . '<br>';
if ($total <= 5) {
/* if( woo_reg_matured( $days_old = 30 ) ) {
echo 'You are inactive with ' . $total . ' orders and account age over 30 days';
return;
}*/
echo 'You are inactive with ' . $total . ' orders and account age under 30 days';
return;
}
echo 'You are active with over 30 days age and ' . $total . ' orders';
return;
endif;
这里有点菜鸟。 我正在尝试编写一些代码来确定当前用户是否有:一个超过 30 天的帐户和 0 个 woocommerce 订单。 我有这个顶部部分在过去 90 天内获取属于用户的所有订单。
然而,它 returns 0 个订单,即使我的测试用户有 1 个已完成的订单。 我有预感可能是这一行: 'before' => 日期('Y-m-d', strtotime('now')) 所以我删除了它,然后它返回了“19 个订单”,这简直是不可能的。 print_r 部分好像只显示了 1 个订单的乱码元数据,所以它从哪里得到 19,我无法理解!
非常感谢对此问题的任何帮助!所有回声和 print_r 都用于调试目的。
function woo_reg_matured( $days_old = 30 )
{
$cu = wp_get_current_user();
return ( isset( $cu->data->user_registered ) && strtotime( $cu->data->user_registered ) < strtotime( sprintf( '-%d days', $days_old ) ) ) ? TRUE : FALSE;
}
function woo_inactive_shortcode( $atts = array(), $content = '' ) {
if ( is_user_logged_in() ):
$customer_orders = get_posts( array(
'numberposts' => - 1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => array( 'shop_order' ),
'post_status' => array( 'wc-completed' ),
'date_query' => array(
'after' => date('Y-m-d', strtotime('-90 days'))
) ) );
$total = 0;
print_r($customer_orders);
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order );
$total += $order->get_total();
}
echo 'Orders: ' . $total . '<br>';
if ($total <= 5) {
if( woo_reg_matured( $days_old = 30 ) ) {
echo 'You are inactive with ' . $total . ' orders and account age over 30 days';
return;
}
echo 'You are inactive with ' . $total . ' orders and account age under 30 days';
return;
}
echo 'You are active with over 30 days age and ' . $total . ' orders';
return;
endif;
}
add_shortcode( 'woo_inactive', 'woo_inactive_shortcode' );
print_r 显示:
Array ( [0] => WP_Post Object ( [ID] => 75675 [post_author] => 1 [post_date] => 2018-01-17 12:16:28 [post_date_gmt] => 2018-01-17 12:16:28 [post_content] => [post_title] => Order – January 17, 2018 @ 12:16 PM [post_excerpt] => [post_status] => wc-completed [comment_status] => open [ping_status] => closed [post_password] => order_5a5f3e9c442d6 [post_name] => order-jan-17-2018-1216-pm [to_ping] => [pinged] => [post_modified] => 2018-01-17 12:17:24 [post_modified_gmt] => 2018-01-17 12:17:24 [post_content_filtered] => [post_parent] => 0 [guid] => https://www.sheersense.com/?post_type=shop_order&p=75675 [menu_order] => 0 [post_type] => shop_order [post_mime_type] => [comment_count] => 3 [filter] => raw ) )
然而 $total
显示:Orders: 19
哪里来的19!!!!哈哈
这样试试。
if ( is_user_logged_in() ):
$customer_orders = get_posts( array(
'numberposts' => - 1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => array( 'shop_order' ),
'post_status' => array( 'wc-completed' ),
'date_query' =>
array('after' =>
array(
'year' => 2017,
'month' => 03,
'day' => 12,
),
'before' =>
array(
'year' => 2018,
'month' => 01,
'day' => 17,
),
) ) );
$total = 0;
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order );
print_r($customer_order);
//$total++;
$total += $order->get_total();
}
echo 'Orders: ' . $total . '<br>';
if ($total <= 5) {
/* if( woo_reg_matured( $days_old = 30 ) ) {
echo 'You are inactive with ' . $total . ' orders and account age over 30 days';
return;
}*/
echo 'You are inactive with ' . $total . ' orders and account age under 30 days';
return;
}
echo 'You are active with over 30 days age and ' . $total . ' orders';
return;
endif;