SQL通过喜欢对wordpress中的嵌套注释进行排序

时间:2018-03-15 21:20:29

标签: sql wordpress comments

我希望有些善良的人可以帮助我。

我想通过喜欢对wordpress中的嵌套评论进行排序。我只找到一个插件来完成这个并且它不能满足我的需求,所以我试着写自己的。其中大部分内容实际上非常简单,但是sql正在逃避我(不是我的力量)。

我需要一个SQL查询来按喜欢对评论进行排序,回复紧跟在其父级之后,并回复每个父级也按喜欢排序。顶级评论和回复按“'层”区分。只有一个级别的回复。我的表看起来像这样:

ID(Int)

Comment_Name(VarChar)

Layer(Int)... 1表示顶级评论,2表示答复

ID_of_Parent_Comment(Int)...回复必须使用此ID分组在顶级评论下

喜欢(Int)

例如,如果顶级注释由数字表示并按字母回复,则它看起来像这样:

1,2,3,3a,3b,4,5,5a ......等

有人有什么想法吗?

2 个答案:

答案 0 :(得分:0)

看起来应该很接近:

select 
  post.ID,
  post.likes as postLikes,
  reply.ID,
  reply.likes as replyLikes
from MyTable post
  left join MyTable reply
    on post.ID = reply.ID_of_Parent_Comment
where post.ID_of_Parent_Comment is  null
order by post.likes desc, reply.likes desc
;

它将为您提供父母喜欢的父ID,以及按最喜欢的孩子排序的每个父母的相关子ID(如果有的话)

答案 1 :(得分:0)

事实证明,其他答案毕竟没有完全奏效。看起来确实如此。回复在适当的父评论下很好地分组,everthing按喜欢排序。但仔细观察,sqlfiddle测试返回了14条记录,其中只有12条可用。

花了太多时间在我的网站上摆弄它,我无法再解决它了。一组或另一组(顶级评论或回复)总是被遗弃或重复。

我终于放弃了,假设它无法用SQL完成,所以我回到了我熟悉的东西:php。这是我的解决方案。希望有人会发现它很有用。如果不出意外,这是一个有趣的项目。

myComments.php

<?php

global $wpdb;

$post_ID = get_the_ID();

// Get Comment Table
$sql =
 " SELECT *"
." FROM wp_comments"
." WHERE comment_post_ID = " . $post_ID     // only retrieve comments for this post
." AND comment_parent = '0'"                // only retrieve top level comments
." ORDER BY likes DESC"
.";";
$tlc = $wpdb->get_results($sql, ARRAY_A);   // Retrieve all records into $tlc
                                            // this should never be
                                            // large enough to be a problem.
$commentCount = count( $tlc );              // Number of TopLevelComments

// Adjust Comments
for ( $i = 0; $i <= $commentCount-1; $i++ ) {
    $tlc[$i]['layer'] = 0;                  // Layer 0 indicates top level comment
    $tlc[$i]['index'] = $i;                 // index is used to group parents 
                                            // with children
}

// Get Reply Table
$sql =
 " SELECT *"
." FROM wp_comments"
." WHERE comment_post_ID = " . $post_ID
." AND comment_parent > '0'"                        // only retrieve replies
." ORDER BY likes DESC"
.";";
$replies = $wpdb->get_results($sql, ARRAY_A);
$replyCount = count( $replies );

// Adjust Replies
for ( $i = 0; $i <= $commentCount-1; $i++ ) {
    $replies[$i]['layer'] = 1;                      // Layer 1 indicates replies
}

// Set child index to that of parent
// then add child record to parent array
for ( $i = 0; $i <= $replyCount-1; $i++ ) {
    $x = $replies[$i]['comment_parent'];            // Get ID of parent
    for ( $j = 0; $j <= $commentCount-1; $j++ ) {
        if ( $tlc[$j]['comment_ID'] == $x ) {       // If parent found
            $value = $tlc[$j]['index'];             // Get parent's index
            $replies[$i]['index'] = $value;         // Give child parent's index
            array_push ( $tlc, $replies[$i]);
        }
    }
}

// Sort comments
// Note that $tlc was sorted by select
// and index was assigned while in that order
$tlc = array_orderby($tlc,  'index', SORT_ASC, 
                            'layer', SORT_ASC,
                            'likes', SORT_DESC);

// Display comments
$commentCount = count($tlc);
if ( $commentCount ) {
    echo "<ol class='commentNumbering'>";
    // Used to determine if we have opened a second <ol> for nested comments
    // and ensure we close it before we are done.
    $inReplyList = false;
    // We don't want to close the <ol> before we've opened it.
    $firstComment = true;
    for ( $i = 0; $i <= $commentCount-1; $i++ ) {
        $myComment = $tlc[$i];
        // Set $depth (needed by reply-link on myCommentTemplate page)
        $depth = 0;
        $comment_ID = $myComment['comment_ID'];
        while( $comment_ID > 0  ) {
            $tempComment = get_comment( $comment_ID );
            $comment_ID = $tempComment->comment_parent;
            $depth++;
        }
        // Treat each group of nested comments as a separate ordered group
        if ( $depth == 2 ) {
            if ( ! $inReplyList ) {
                echo "<ol>";
                $inReplyList = true;
            }
        } else {
            if ( ! $firstComment ) {
                if ( $inReplyList ) {
                    echo "</ol>";
                    $inReplyList = false;
                }
            }
        }
        $firstComment = false;
        // Display each comment
        include ('myCommentTemplate.php');
    }
    if ( $inReplyList ) {
        echo "</ol>";
    }
    echo "</ol>";
} else {
    echo 'No comments found.';
}
// Where comments are made
include('myCommentForm.php');

$wpdb->flush();

?>

function array_orderby()(位于functions.php中)

/* SORT PHP ARRAYS OF RECORDS */

// PHP function 'array_multisort' requires columns //
// This function handles the conversion from row to col and back again //

// Example:
// $sorted = array_orderby($data, 'volume', SORT_DESC, 'edition', SORT_ASC);

function array_orderby()
{
    $args = func_get_args();
    $data = array_shift($args);
    foreach ($args as $n => $field) {
        if (is_string($field)) {
            $tmp = array();
            foreach ($data as $key => $row)
                $tmp[$key] = $row[$field];
            $args[$n] = $tmp;
            }
    }
    $args[] = &$data;
    call_user_func_array('array_multisort', $args);
    return array_pop($args);
}