WordPress.org

Ready to get started?Download WordPress

Forums

Show only oldest post by author (5 posts)

  1. adamrice
    Member
    Posted 2 years ago #

    I'm hacking around with a complicated configuration that involves custom post types and custom taxonomies in a multi-author environment (but not WPMU).

    I am creating an archive template for the custom post type, and I want to set it up so that if an author has created more than one post, it should only show the oldest one.

    My current loop looks like this

    $args = array
    	('post_type' => 'ticketlisting',
    		'posts_per_page' => 50,
    		'tax_query' => array
    		(array
    			(
    			'taxonomy' => 'tier',
    			'field' => 'slug',
    			'terms' => 'goldstar'
    			)
    		)
    	);
    
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
    	the_title();
    	echo '<div class="entry-content">';
    	$post_id = get_the_ID();
    	/* show post content here */
    	echo '</div>';
    endwhile;
  2. tugbucket
    Member
    Posted 2 years ago #

    $args= array(
    	'orderby' => 'date',
    	'order' => 'ASC',
    	'posts_per_page' => 1
    );
  3. adamrice
    Member
    Posted 2 years ago #

    Thanks.

    Will that get me the oldest post from each author, or just the oldest post from among all authors? I want to do the former, but it looks like it does the latter.

  4. tugbucket
    Member
    Posted 2 years ago #

    $authors = $wpdb->get_results("SELECT ID, user_nicename, display_name from $wpdb->users " . ($exclude_admin ? "WHERE user_login <> 'admin' " : '') . "ORDER BY display_name");
    foreach($authors as $author){
    	$numposts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_author = " . $author->ID . " ORDER BY post_date ASC LIMIT 1 ");
    	echo '<ul>';
    	echo '<li>Author: ' . $author->display_name . '</li>';
    	foreach ($numposts as $numpost) {
    		echo '<li>Oldest Post: '.$numpost->post_title.'</li>';
    	}
    	echo '</ul>';
    }
  5. adamrice
    Member
    Posted 2 years ago #

    Awesome, thanks. I'll try to combine that with my other finicky selection criteria.

Topic Closed

This topic has been closed to new replies.

About this Topic