Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am creating a website so that users can submit their articles from the front end. I have quite a few things set up:

  • The ability to submit custom post types from the front end
  • Blocked from the back-end.

I'd like to create a page (for logged in users) labeled "my posts" or my "submissions" that will show logged in users their own custom post post types and their status so that they know what's been published, what will be published in the future, pending, drafts (so they can go back and edit), and pending (so that I as Editor, know when an article is ready for my review).

I'm guess I'm looking for a frontend user dashboard. I've tried WP User Frontend, but it clashes with s2member and won't show posts from s2member registered users.

I just need something simple that will allow users easy access from the front end to manage their posts. I've been trying plugin after plugin and adding things to my short code creator but nothing works the way I need it to.

It doesn't have to be pretty, just a simple code that a non coder (like me) update per by custom post types.

To explain further, i'd like to have one code on a page shows the users all the deals they've submitted (and their status), and another code to show users all the articles they've submitted (and their status), etc.

Any help would be very useful

Thanks in advance!

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

I have Something that i use from time to time when i need something like that:

<?php
/*
Plugin Name: List User Posts
Plugin URI: http://en.bainternet.info
Description: lists user posts on the front end
Version: 0.1
Author: Bainternet
Author URI: http://en.bainternet.info
*/

if (!class_exists('list_user_posts')){
    /**
    * list_user_posts
    * @author Ohad Raz
    */
    class list_user_posts
    {
        /**
         * __construct class constructor
         * 
         * @author Ohad Raz
         * @param array $args
         */
        function __construct($args = array())
        {
            add_shortcode('user_posts', array($this,list_user_posts));
        }

        /**
         * list_user_posts shortcode handler
         * 
         * @author Ohad Raz
         * @param  array  $attr    shortcode attributes
         * @param  string $content shortcode content
         * @return string
         */
        public function list_user_posts($attr = array(), $content = null)
        {
            extract(shortcode_atts(array(
                    'post_type' => 'post',
                    'number' => 10,
                ), $attr));

            //if the user is not logged in the give him a link to log in
            if (!is_user_logged_in()){
                return sprintf(__('You Need to <a href="%s">Login</a> to see your posts'),wp_login_url(get_permalink()));
            }
            //this is for pagination
            $pagenum = isset( $_GET['pagenum'] ) ? intval( $_GET['pagenum'] ) : 1;

            //get user's posts
            $args = array(
                'author' => get_current_user_id(), //this makes the query pull post form the current user only
                'post_status' => array('draft', 'future', 'pending', 'publish'),
                'post_type' => $post_type,
                'posts_per_page' => $number,
                'paged' => $pagenum
            );
            $user_posts = new WP_Query( $args );

            $retVal = '';
            if ( $user_posts->have_posts() ) {

                //set table headers
                $retVal = '
                    <table class="user-posts-table" cellpadding="0" cellspacing="0">
                        <thead>
                            <tr>
                                <th>'.__( 'Title', 'lup' ).'</th>
                                <th>'.__( 'Status', 'lup' ).'</th>
                                <th>'.__( 'Actions', 'lup' ).'</th>
                            </tr>
                        </thead>
                        <tbody>';
                //loop over and add each post to the table
                global $post;
                $temp = $post;
                while ($user_posts->have_posts()){
                    $user_posts->the_post();
                    $title = $post->post_title;
                    $link = '<a href="'.get_permalink().'" title="'.sprintf( esc_attr__( 'Permalink to %s', 'lup' ), the_title_attribute( 'echo=0' ) ).'" rel="bookmark">'.$title.'</a>';
                    $retVal .= 
                            '<tr>
                                <td>
                                    '.( in_array( $post->post_status, array('draft', 'future', 'pending') ) ? $title : $link).'
                                </td>
                                <td>
                                    '.$post->post_status .'
                                </td>
                                <td>
                                    <a href="LINK_TO_YOUR_EDIT_PAGE"><span style="color: green;">'. __( 'Edit', 'lup' ).'</span></a>
                                    <a href="LINK TO YOUR DELETE PAGE"><span style="color: red;">'.__( 'Delete', 'lup' ).'</span></a>
                                </td>
                            </tr>';
                }
                $retVal .= '</tbody></table>';

                //create pagination (if needed)
                if ($user_posts->found_posts > $number ){
                    $pagination = paginate_links( array(
                        'base' => add_query_arg( 'pagenum', '%#%' ),
                        'format' => '',
                        'prev_text' => __( '&laquo;', 'lup' ),
                        'next_text' => __( '&raquo;', 'lup' ),
                        'total' => $user_posts->max_num_pages,
                        'current' => $pagenum
                        ) 
                    );
                    if ( $pagination ) {
                        $retVal .= '<div class="pagination">'.$pagination .'</div>';
                    }
                }
                //return table of posts
                return $retVal;
            }else{
                //  no posts for this users found
                return  __("No Posts Found");
            }
        }

    }//end list_user_posts class
}//end if
new list_user_posts();

Its commented all over the place so you can understand whats going on there.

Usage simply create a page and add this shortcode: [user_posts] which will give you a list of posts by the logged in user.

You can also change the post type like so: [user_posts post_type="deal" number="5"]

share|improve this answer
    
Thank you! Where exactly am I supposed to insert this? am I adding this to an existing php file or creating a new one? –  Trce Oct 26 '12 at 22:09
    
This would go in your themes's functions.php file or in a plugin. –  Bainternet Oct 27 '12 at 7:29
    
Bainternet, thank you so much! This looks great only two issues: I'm not sure what to do with the Edit and delete buttons. I tried edit_post_link and delete_post_link, but these aren't working, Not sure what how I'm supposed to fix these. If I can't fix these edit and delete buttons I can't use this. Thanks in advance! –  Trce Oct 27 '12 at 17:12
    
Hmmm, the links should point to the pages where they can edit the posts or delete them, for example to delete wp-snippets.com/delete-post-from-front-end –  Bainternet Oct 27 '12 at 19:50
    
Thank you so much! I just used the .get_permalink($current), this leads them directly to the front end article and I have front end editor plug-in so it works. This is exactly what I needed. So thanks a bunch! –  Trce Oct 28 '12 at 1:23
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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