查询元数据以显示订单。 Woocommerce

时间:2016-09-14 04:35:29

标签: wordpress woocommerce

我正在尝试使用query->set()与我的客户metadata一起显示订单。元数据 - _admin_name存储在wp_postmeta中,我希望管理员只能查看admin_nameuser name相同的订单。

如何将query->set()用于我的元数据?

这是我的代码:

function mypo_parse_query_useronly( $wp_query ) {
    global $post;

    if ( $wp_query->is_admin && strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php?post_type=shop_order') == true) {

        add_action( 'views_edit-post', 'child_remove_some_post_views' );
        global $current_user;
        $userName = $current_user->user_login;
        $meta_query_args = array(
                array(
                        'key'     => '_user_name',
                        'value'   => $userName,
                        'compare' => '='
                )
        );

       //trying use this code
       // $query = new WP_Query( $meta_query_args );


        $wp_query->set( 'author', $current_user->id );

    }
}

add_filter('parse_query', 'mypo_parse_query_useronly' );

  //don't display the order whish is not own by current user.
 function child_remove_some_post_views( $views ) {
    //header('Location: '.$newURL);
    unset($views['all']);
    unset($views['wc-processing']);
    unset($views['wc-on-hold']);
    unset($views['wc-completed']);
    unset($views['pending']);
    return $views;
}

有这样做吗?

感谢。

1 个答案:

答案 0 :(得分:1)

<?php
function mypo_parse_query_useronly( $wp_query ) {
    global $post;

    if ( $wp_query->is_admin && strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php?post_type=shop_order') == true) {

        add_action( 'views_edit-post', 'child_remove_some_post_views' );
        global $current_user;
        $userName = $current_user->user_login;
        $meta_query_args = array(
                array(
                        'key'     => '_user_name',
                        'value'   => $userName,
                        'compare' => '='
                )
        );

       //trying use this code
       // $query = new WP_Query( $meta_query_args );


        $wp_query->set( 'author', $current_user->id );
        $wp_query->set( 'meta_query', array(
                                        array(
                                              'key' => '_user_name',
                                              'value' => $userName,
                                              'compare' => '='
                                        )
                         ));

    }
}
add_filter('parse_query', 'mypo_parse_query_useronly' );
//don't display the order whish is not own by current user.
 function child_remove_some_post_views( $views ) {
    //header('Location: '.$newURL);
    unset($views['all']);
    unset($views['wc-processing']);
    unset($views['wc-on-hold']);
    unset($views['wc-completed']);
    unset($views['pending']);
    return $views;
}