wordpress 如何从保存的列表中排除当前用户

t0ybt7op  于 2023-04-29  发布在  WordPress
关注(0)|答案(2)|浏览(138)

我正在尝试从保存的列表中排除当前用户。我的代码包含了每个保存了用户的用户,但我不希望当前用户出现在列表中。如何排除当前用户?这是我的代码。

<?php
$sql              = "SELECT * FROM $wpdb->posts WHERE post_type IN ('" . $directory_url_1 . "','" . $directory_url_2 . "' ) and post_author='" . $current_user->ID . "' and post_status IN ('publish','pending','draft' )  ";
$authpr_post      = $wpdb->get_results( $sql );
$total_post       = count( $authpr_post );
$iv_redirect_user = get_option( '_iv_directories_profile_public_page' );
$reg_page_user    = '';
if ( $iv_redirect_user != 'defult' ) {
    $reg_page_user = get_permalink( $iv_redirect_user );
}
if ( $total_post > 0 ) {
    $i = 0;
    foreach ( $authpr_post as $row ) {        //echo '<br/>ID...'.$row->ID;
        $user_list      = get_post_meta( $row->ID, '_favorites', TRUE );
        $user_list_arr2 = [];
        $user_list_arr  = array_filter( explode( ",", $user_list ), 'strlen' );
        $i              = 0;
        foreach ( $user_list_arr as $arr ) {
            if ( trim( $arr ) != '' ) {
                $user_list_arr2[ $i ] = $arr;
                $i ++;
            }
        }
        if ( sizeof( $user_list_arr2 ) > 0 ) {

            $args_users = ['include' => $user_list_arr2,];
            // The User Query
            $user_query = new WP_User_Query( $args_users );

            if ( ! empty( $user_query->results ) ) {
                foreach ( $user_query->results as $user ) {
                    //print_r( $user );                                                 
                    ?>

我试着添加这个字符串,但它没有工作。哦,我也很新,在这方面,可能没有添加它正确。

$filtered_user = user.objects.exclude(id=request.user.id)
$print("ALL USERS:" + str(filtered_user))

args = {
  'user': user,
  $'filtered_user':
r3i60tvu

r3i60tvu1#

由于您已经在使用array_filter,您可以扩展过滤器并删除一些代码,同时排除当前用户:

if ( $total_post > 0 ) {
    $i = 0;  // is this necessary?? 
    foreach ( $authpr_post as $row ) {        //echo '<br/>ID...'.$row->ID;
        $user_list      = get_post_meta( $row->ID, '_favorites', TRUE );
        // REMOVE $user_list_arr2 = [];
        // SUBSTITUTE $user_list_arr  = array_filter( explode( ",", $user_list ), 'strlen' );
        $user_list_arr = array_filter(explode(',', $user_list), fn ($v) => ($tv = trim($v)) && $tv != $current_user->ID);
        
        /*  REMOVE
        $i              = 0;
        foreach ( $user_list_arr as $arr ) {
            if ( trim( $arr ) != '' ) {
                $user_list_arr2[ $i ] = $arr;
                $i ++;
            }
        }
        */
        
        // Now you must use $user_list_arr instead of $user_list_arr2
        
        if ( sizeof($user_list_arr) > 0 ) {  // you could use count($user_list_arr) as you did for $total_post

            $args_users = ['include' => $user_list_arr];
            // The User Query
            $user_query = new WP_User_Query( $args_users );

            if ( ! empty( $user_query->results ) ) {
                foreach ( $user_query->results as $user ) {
                    //print_r( $user );
8i9zcol2

8i9zcol22#

  • 我从来没有做过WordPress开发者。
  • 有数据库的迭代行程--这些不是最佳实践;应该改为实现表JOINing。
  • 数据库表中有逗号分隔的值--这不是最佳实践;数据库应该被规范化。
  • 我下面的代码片段没有经过测试。

1.使用准备好的语句获取所有符合条件的帖子。
1.迭代这些结果并使用preg_split()构建include数组。
1.构建正则表达式模式以消除分隔逗号旁边的潜在空格,并将当前用户的id视为分隔符,以便它不包含在返回的数组中。
1.使用PREG_SPLIT_NO_EMPTY()过滤掉任何空值。
验证码:

$current_user_posts = $wpdb->get_results(
    $wpdb->prepare(
        <<<SQL
            SELECT *
            FROM $wpdb->posts
            WHERE post_author=%d
                  AND post_type IN (%s,%s)
                  AND post_status IN ('publish','pending','draft')
        SQL,
        [$current_user->ID, $directory_url_1, $directory_url_2]
    )
);

foreach ($current_user_posts as $post) {
    $args_users = [
        'include' => preg_split(
            "/ *, *|\b{$current_user->ID}\b/",
            trim(get_post_meta($post->ID, '_favorites', TRUE)),
            0,
            PREG_SPLIT_NO_EMPTY
        )
    ];
    foreach ((new WP_User_Query($args_users))->get_results() as $user) {
        // do whatever you need to do
    }
}

相关问题