• Resolved fixer1

    (@fixer1)


    Hi there,

    I’ve spending most of my day trying to sort this out:

    I’m using a twentytwelve child theme. I’ve added a Custom Post Type ‘People’ to which I’ve added a couple of posts.

    I want to show these posts on the archive page http://www.domain.com/people/ in alphabetical order and I want to limit the number of posts on the page to 5.

    Now I DON’T want this to affect the way other post types are shown, only those posts of the custom post type ‘People’.

    I’ve been trying all kinds of tweaks but I can’t get it to work properly. Some tweaks mess up the pagination, some don’t seem to work at all.

    I have made a archive-people.php so I can make the changes in there.

    What is the cleanest way to do this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this in your theme’s functions.php:

    function my_post_queries( $query ) {
    	// not an admin page and is the main query
    	if ( !is_admin() && $query->is_main_query() ) {
    		// maybe you have to change 'People' to 'people'
    		if ( is_post_type_archive( 'People' ) ) {
    
    			$query->set( 'posts_per_page', 5 );
    			$query->set( 'orderby', 'title' );
    			$query->set( 'order', 'ASC' );
    
    		}
    	}
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    http://www.billerickson.net/customize-the-wordpress-query/

    Thread Starter fixer1

    (@fixer1)

    Hi,

    thanks for the quick response. I found this code as well, looks the same for the most part. Any reasons why I should use this code or yours instead?

    // Limit posts per page and sort by title - archive 'movie' page
    add_action( 'pre_get_posts', 'archive_movie_page_of_posts' );
    
    function archive_movie_page_of_posts ( $query ) {
    	    // makes sure admin pages aren't affected
    	if ( is_admin() || ! $query->is_main_query() )
            return;
    
        if ( is_home() ) {
            // Display only 1 post for the original blog archive
            $query->set( 'posts_per_page', 1 );
            return;
        }
    
        if ( is_post_type_archive( 'movie' ) ) {
            // Display 3 posts for the specified custom post type
            $query->set( 'posts_per_page', 3 ); // use '-1' to show all results on one page
    		$query->set( 'orderby' , 'title' );
    		$query->set( 'order' , 'asc' );
            return;
        }
    }
    add_action( 'pre_get_posts', 'archive_movie_page_of_posts', 1 );

    Thanks!

    Moderator keesiemeijer

    (@keesiemeijer)

    They are pretty much the same, but your code also sets the posts on your home page to one post.

    if ( is_home() ) {
            // Display only 1 post for the original blog archive
            $query->set( 'posts_per_page', 1 );
            return;
        }

    http://codex.wordpress.org/Function_Reference/is_home
    http://codex.wordpress.org/Function_Reference/is_post_type_archive

    Try them both, there is not much difference.

    Thread Starter fixer1

    (@fixer1)

    Thanks for the quick help!

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome. I’m glad you’ve got it resolved.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Sort posts alphabetically and limit posts in TwentyTwelve archives only’ is closed to new replies.