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 have a custom post type setup called website I thought using the code below would display only the logged in users posts but it's displaying all of the custom posts even those not belonging to the logged in user. Can someone show me what the query is to get only the custom posts by the logged in user

global $current_user;
      get_currentuserinfo();
$authorID = $current_user->ID;
    $args = array(
              'post_type' => 'post',//or whatever you need
              'posts_per_page' => 5,
              'author' => $authorID
              );

    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query($args);
share|improve this question
add comment

1 Answer

Create a small plugin to add your custom post type to the default loop, if the user is logged in.

// add custom post type to wp loop
add_filter( 'pre_get_posts', 'fb_add_to_query' );

// ads to query
function fb_add_to_query( $query ) {

    if ( ! is_user_logged_in() ) // if user not logged in, return
        return $query;

    if ( is_admin() || is_preview() ) // return, if in backend or preview
        return $query;

    if ( ! isset( $query -> query_vars['suppress_filters'] ) )
        $query -> query_vars['suppress_filters'] = FALSE;

    // conditional tags for restrictions
    if ( is_home() || is_front_page() && ( FALSE == $query -> query_vars['suppress_filters'] ) ) 
        $query->set( 'post_type', array( 'post', 'my_post_type' ) );

    return $query;
}
share|improve this answer
    
This isn't for the home page though, it's in a custom page template. –  Anagio Dec 21 '12 at 8:37
    
You must change the conditional tags for restrictions and ready, then works only on your page tamplate. –  bueltge Dec 21 '12 at 8:57
    
I tried setting it to this ` if ( is_page() && ( FALSE == $query -> query_vars['suppress_filters'] ) ) ` but it doesn't display any posts at all. –  Anagio Dec 21 '12 at 13:15
    
Is this to go into functions.php or in my page template? If in functions.php what is the code to play into my page template? Thanks –  Anagio Dec 21 '12 at 15:07
    
Use as plugin or functions.php. –  bueltge Dec 21 '12 at 17:06
show 2 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.