• We have created a custom plugin where we are displaying posts list in custom format. I want to add a condition like if the current logged in user role is author and then he can see list of only his posts. If current logged in user role is admin/editor then he should be able to see list of all posts of all authors.
    
    I am adding here the snippet of our code -
    
                function sort_posts_by_title_descending(){
    
                            var articles = [];
    
                            <?php
    
                        $args = array(
    
                                                    'post_status' => get_post_stati(),
    
                            'orderby' => 'TITLE',
    
                            'numberposts' => -1,
    
                            'order' => 'DESC',
    
                                        );
    
                        $posts = get_posts($args);
    
                        if ($posts) {
    
                            foreach ( $posts as $post ) { ?> // exit PHP
    
                                var obj = {}; // Create a JS object
    
                                title = "<?php echo esc_js(strval($post->post_title))?>";
    
                                post_status = "<?php echo esc_js(strval($post->post_status))?>";
    
                                obj['title'] = title;
    
                                obj['post_status'] = post_status;
    
        obj['user_login'] = "<?php echo  esc_js(get_user_by('id', $post->post_author)->user_login);       
    
          ?>";
    
                                obj['article_id'] = "<?php echo esc_js($post->ID) ?>";
    
                                obj['post_author'] = "<?php echo esc_js($post->post_author) ?>";
    
                                obj['post_date'] = "<?php echo 'Last Modified '.esc_js($post->post_date); ?>";
    
                                <?php
    
                                $user_meta = get_userdata(get_current_user_id());
    
                                $user_roles = $user_meta->roles ? $user_meta->roles[0] : false;
    
                                if ('author' == $user_roles && get_current_user_id()!=$post->post_author) {
    
                                                                            continue;
    
                                                                } else { ?>
    
                                        articles.push(obj);
    
                                <?php }
    
                            }
    
                        }
    
                    ?>
    
                    return articles;
    
                }
    
    I have added the code which starts from $user_meta = . Whenever the page where lists of posts is displayed in custom format renders I can see that in database Auto-Draft post is created which is displayed on the page as well. But if I open wordpress default posts display page it doesnot display the newly created Auto-draft post there.
    
    I am not able to understand why a Auto Draft post is getting created because of addition of my code.
    
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Try including 'post_status' => 'publish', as part of your query args. This should be the default arg and you shouldn’t need it, but something else is altering the query var if you are getting auto-save posts. Hopefully that something else is coded well enough to not mess with explicitly set query vars.

    If doing this does not help, you’ll need to either find out what’s altering the query vars and correct it, or hook “pre_get_posts” with a large priority arg and set the query var that way. This should override what that something else is doing. However, doing so could negatively impact other queries where auto-drafts are needed. Make an effort to only alter your specific query and leave all others alone.

Viewing 1 replies (of 1 total)
  • The topic ‘Auto-Draft posts getting created Automatically’ is closed to new replies.