0
votes

What I'm trying to achieve:

Topic (1)

Reply to 1 (2)

--Reply to 2 (3)

--Reply to 2 (4)

----Reply to 4 (5)

----Reply to 4 (6)

Reply to Topic 1 (7)

----Reply to 6 (7)

------Reply to 7 (8)

---Reply to 8 (9)

------Reply to 8 (10)

What I currently have is more like

Topic (1)

Reply to 1 (2)

Reply to 2 (3)

Reply to 4 (5)

Reply to 7 (6)

Reply to 6 (7)

Reply to 4 (8)

Reply to 8 (9)

Reply to Topic 1 (10)

Reply to 8 (11)

Reply to 2 (12)

Above is a sample structure of a forum I built using Wordpress and bbPress. There are topics and replies, both which are custom post types. I'm able to display the topics with replies fine, but I wish to sort them in a nested/threaded view. Rather than simply having the replies all appear in order of date or id, I want them to appear nested beneath the reply which is the parent.

I hope that makes sense. Basically it is threaded topics and replies just like facebook does.

Below is my args and query for the replies, which is inside the topic WP_Query loop.

            $args = array(
                'post_type'         => 'reply', // custom post type
                'posts_per_page'    => '50',
                'orderby'       => 'post_parent',
                'post_parent'       => $topic_id,

                 'meta_query' => array(
                     'relation' => 'OR',
                     array(
                       'key'=> $topic_id,
                    ),
                    array(
                       'key'=>'_bbp_reply_to',
                       'compare' => 'NOT EXISTS'
                   ),
                     array(
                         'key'=>'_bbp_reply_to',
                         'compare' => 'EXISTS',
                     ),
                 ),
                'orderby' => 'meta_value_num',
                 'order' => 'ASC',
            );

                //global $wpdb;

            $loopReply = new WP_Query($args);

I hope this isn't too unclear. I've found other close questions regarding wordpress postmeta sorting, but this is an odd one. You see the meta key _bbp_reply_to ONLY shows up if it is a reply to a reply. It doesn't happen if it is a reply to a topic. This is why I "think" I need to array key exists portion of the args.

IN SHORT

Need a set of $args, if possible to do nested replies. Or do I have to do my own manual sql to achieve this?

EDIT: See my comment. I changed the title a bit as I believe to achieve this a custom Wordpress Walker Class implementation is likely the solution.

1
I have not solved this yet but after a lot of reading I've determined the best way to achieve this is with a Wordpress Walker class. Hopefully someone who knows WP Walkers can provide some input. I'll post my solution if I figure it out before an answer is posted. - mediaguru
Still banging my head against the wall on this, but a recursive function may also be an option. I'm working on that, but the recursion isn't happening yet. - mediaguru

1 Answers

0
votes

I've found a solution which works great. I ended up copying the bbPress Reply Walker class which was an extension of the WordPress walker class, and modifying some of the parameters to meet my needs. I had to also make my own function for listing replies, similar to bbPress's list replies function. The list replies function had custom args as follows:

                $args = array(
                    'post_type'           => 'reply',                   
                    'post_parent'         => $postID,                   
                    'posts_per_page'      => 50,                        
                    'orderby'             => 'date',                        
                    'order'               => 'ASC',                         
                    'hierarchical'        => true,
                    'ignore_sticky_posts' => true,                          
                )