WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Bug with Custom Post Types and Galleries (2 posts)

  1. zackaustin
    Member
    Posted 4 years ago #

    Hello there,
    I am a sort of amateur theme developer but i have noticed one thing in my runs in trying to integrate custom post types into my site.

    I have set up a custom post type called "Galleries" and it is to hold posts, that contain just a gallery of the images attached to the post.

    I have a few posts up, with dummy content under a regular 'post' post type, and then a few galleries up under a 'gallery' post type.

    I am using the following code to pass the 'gallery' post type into the query.

    add_filter( 'pre_get_posts', 'add_gallery_post_type' );
    
    function add_gallery_post_type( $query ) {
    
    	if ( is_home() || is_feed() )
    		$query->set( 'post_type', array( 'post', 'gallery' ) );
    
    	return $query;
    }

    But for some reason, posts with the gallery shortcode do not work, and the gallery is not loaded. I am not too sure whether there is a glitch, or if im adding the post type
    incorrectly causing the gallery shortcode to break.
    Thanks!

  2. zackaustin
    Member
    Posted 4 years ago #

    I did some fooling around, and i think i fixed it.
    when you set the query to post_types post, and gallery you also need to include attachment otherwise they will not load in!
    problem solved!

    New code:

    add_filter( 'pre_get_posts', 'add_gallery_post_type' );
    
    function add_gallery_post_type( $query ) {
    
    	if ( is_home() || is_feed() )
    		$query->set( 'post_type', array( 'post', 'gallery', 'attachment' ) );
    
    	return $query;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic