0

I'm not keen on private posts interspersed with published posts on the front end, so I'm writing a plugin that seeks to segregate and separate them out.

As part of this I want to modify the post navigation on single post views so that: if the post is published, post navigation shows only published posts. if the post is private, post navigation shows only private posts.

This is how far I've got, it's plainly incomplete but I could do with some pointers about how to proceed.

/*
 *filter get_adjacent_post
 */

public function get_next_post_mod($where){
        if (is_single()){
                global $wpdb, $post;
                if ( get_post_status ( ) == 'private' ) {
                        $new_where = "WHERE p.post_type = 'post' AND p.post_status = 'private'";
                        return $new_where;
                } else {
                        $new_where = "WHERE p.post_type = 'post' AND p.post_status = 'publish'";
                        return $new_where;
                }

        }

}
public function get_previous_post_mod($where){
        if (is_single()){
                global $wpdb, $post;
                if ( get_post_status ( ) == 'private' ) {
                        $new_where = "WHERE p.post_type = 'post' AND p.post_status = 'private'";
                        return $new_where;
                } else {
                        $new_where = "WHERE p.post_type = 'post' AND p.post_status = 'publish'";
                        return $new_where;
                }

        }

}

the filters are added to the relevant functions in the constructor function of my class, and work insofar as they achieve the separation by post status, but obviously the filter misses some essential points which mean it's not getting the adjacent posts correctly.

I have had a look at the source of the functions in wp core, as well as digging around related questions on these fora. I can see clues and options of what to try. Just looked at this question which was interesting and potentially helpful, particularly as an example of how I could make this DRY, but not really the same situation.

Could anyone give me some pointers? I presume I'm missing something in the WHERE statements.

I'd just like to get it working for now and then look at re-factoring since it's a personal project.

0

Well it wasn't too hard after some perspective.

public function get_adjacent_post_mod($where){
        if (is_single()){
            global $wpdb, $post;
            if ( get_post_status ( ) == 'private' ) {
                $where = str_replace( "AND ( p.post_status = 'publish' OR p.post_status = 'private' )", "AND p.post_status = 'private'", $where );
                return $where;  
            } else {
                $where = str_replace( "AND ( p.post_status = 'publish' OR p.post_status = 'private' )", "AND p.post_status = 'publish'", $where );
                return $where;  
            }
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.