Peter
Peter

Reputation: 11835

Wordpress - Get latest custom post from every user

how can a get the latest custom post from every user?

$args = array(  'post_type'      => 'userdatax', 
                'post_status'    => 'publish', 
                'orderby'        => 'post_date',
                'order'          => 'DESC',    
                'posts_per_page' => 999999 );  

$query_res = new WP_Query($args);

Upvotes: 7

Views: 1654

Answers (4)

Gaurav Mittal
Gaurav Mittal

Reputation: 1

<?php
$user_id = get_current_user_id();
$args = array( 'author' => $user_id, 'post_type' => 'any' );

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
    /* Restore original Post Data */
    wp_reset_postdata();
} else {
    // no posts found
}

?>

Upvotes: 4

Akshay Shah
Akshay Shah

Reputation: 3504

according to me below code can achieve your goal.

try this code

function getUserPosts()
{
    $args = array(
    'order'        => 'ASC',
    ); 
    $users = get_users( $args );

    foreach ($users as $key => $value) {
        // WP_Query arguments
        $args = array(
        'post_type'              => array( 'userdatax' ),
        'post_status'            => array( 'publish' ),
        'author'                 => $value->ID,
        'posts_per_page'         => '-1',
        'order'                  => 'DESC',
        'orderby'                => 'date',
        );

        // The Query
        $query = new WP_Query( $args );

        // The Loop
        if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();
            // do something
            echo the_title();
        }
        } else {
        // no posts found
        }

        // Restore original Post Data
        wp_reset_postdata();
    }
}
add_action('init','getUserPosts');

Upvotes: 7

Athul Nath
Athul Nath

Reputation: 2606

I think you need to display the latest post of each user.`

<?php 	
$lastposts = get_posts( array(
	 'post_status'    => 'publish', 
	 'orderby'        => 'post_date',
	 'order'          => 'DESC',
    'posts_per_page' => -1
) );
 
//Code to check only the latest post from  each user is displayed.
if ( $lastposts ) {
	$auther=""; 
    foreach ( $lastposts as $post ) :
        setup_postdata( $post ); 
		  	if($auther!=get_the_author()) { ?>
				<!--Do your html code here -->
				<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
				<?php the_content(); 
				$auther=get_the_author();	
			}
	 
    endforeach; 
    wp_reset_postdata();
}
?>

Hope it Helps :)

Upvotes: 4

omar kasem
omar kasem

Reputation: 129

just change posts_per_page to be -1 like that

 $args = array(  'post_type'      => 'userdatax', 
            'post_status'    => 'publish', 
            'orderby'        => 'post_date',
            'order'          => 'DESC',    
            'posts_per_page' => -1);  

 $query_res = new WP_Query($args);

Upvotes: 4

Related Questions