¿Filtrar la lista de comentarios del administrador para mostrar solo los comentarios del usuario actual?

10

En la página de comentarios ( /wp-admin/edit-comments.php ), todos los usuarios registrados pueden ver todos los comentarios del sitio.

Me gustaría que los usuarios vean solo sus propios comentarios y los comentarios que quedan en sus publicaciones.

¿Cómo puedo filtrar esto?

    
pregunta moonvader 27.06.2012 - 10:40

1 respuesta

9

Cualquiera de estos 3 te ayudará:

//Before getting the comments, on the WP_Comment_Query object for each comment
add_action('pre_get_comments', 'wpse56652_filt_comm');

//Applied on the comments SQL Query, you can modify the 'Where' part of the query
add_filter('comments_clauses', 'wpse56652_filt_comm');

//After the comments are fetched, you can modify the comments array
add_filter('the_comments', 'wpse56652_filt_comm');

function wpse56652_filt_comm($param) {
    //access the current user
    global $current_user;
    get_currentuserinfo();

    //current users id = $current_user->ID;

    //Current users posts, check get_posts params to change as per your need
    $user_posts = get_posts(array('author' => $current_user->ID, 'posts_per_page' => -1));

    echo '<pre>';
    print_r($param);
    echo '</pre>';

    return $param;
}

Además, puede usar global $pagenow para asegurarse de que el código se ejecute solo en esta página.

Lo siento, no estoy bien hoy, ¡así que no pude anotar un ejemplo! ;)

Editar:

/**
 * Show only the Comments MADE BY the current logged user
 * and the Comments MADE TO his/hers posts.
 * Runs only for the Author role.
 */

add_filter('the_comments', 'wpse56652_filter_comments');

function wpse56652_filter_comments($comments){
    global $pagenow;
    global $user_ID;
    get_currentuserinfo();
    if($pagenow == 'edit-comments.php' && current_user_can('author')){
        foreach($comments as $i => $comment){
            $the_post = get_post($comment->comment_post_ID);
            if($comment->user_id != $user_ID  && $the_post->post_author != $user_ID)
                unset($comments[$i]);
        }
    }
    return $comments;
}
    
respondido por el Rutwick Gangurde 27.06.2012 - 11:44

Lea otras preguntas en las etiquetas