• I have a site where Im using custom post types extensively.
    I need to display custom post types on my homepage and my archive/category pages. I tried altering this snippet to display custom post types on home AND archive pages. but its not working correctly?

    How do I make this work for archive pages as well as home?

    add_filter( 'pre_get_posts', 'my_get_posts' );
    
    function my_get_posts( $query ) {
    	if ( ( is_home() || is_archive() && false == $query->query_vars['suppress_filters'] )  )
    		$query->set( 'post_type', array( 'tutorials' ) );
    
    	return $query;
    }

    If I remove the || is_archive() part, it works without error on homepage, which is the exact code I got from Justin Tadlock’s blog.

Viewing 1 replies (of 1 total)
  • Thread Starter isaachorton

    (@isaachorton)

    This works.

    add_filter( 'pre_get_posts', 'my_get_posts' );
    
    function my_get_posts( $query ) {
    	if ( ( is_home() && false == $query->query_vars['suppress_filters'] ) )
    		$query->set( 'post_type', array( 'tutorials' ) );
    	if ( ( is_archive() && false == $query->query_vars['suppress_filters'] ) )
    		$query->set( 'post_type', array( 'tutorials' ) );
    
    	return $query;
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Showing only custom post types on home and archive pages – using functions.php’ is closed to new replies.