WordPress.org

Ready to get started?Download WordPress

Forums

I want to intermingle custom post types and posts more closely (3 posts)

  1. scormeny
    Member
    Posted 2 years ago #

    I am working on a web site where we have both regular posts, and a custom post type for events. The "events" post type is fantastic, and I love it, as it contains all sorts of extra fields for date, location, RSVP list, etc. etc.

    However, my problem is that I want event post types to be more closely linked to regular posts, for my users. This means that I would like to have:

    - categories for my event post types, that are identical to the categories for my regular posts (if they could share the same taxonomy/categories, I feel like that would be the most convenient from a site-management perspective, but from an underlying-structure perspective that may be asking too much or introducing other problems.).

    - category pages that include event post types along with regular posts.

    - search results that include both event post types and regular posts.

    This seems like a tall order, and like it somehow is antithetical to the custom post type idea.

    I'd appreciate any guidance or thoughts on how to make this work. If you do this sort of work for hire, I would be happy to follow up further.

  2. Felix Renicks
    Member
    Posted 2 years ago #

    Add this to the functions.php file in your theme folder. This will list events anywhere posts are listed (assuming the name of your custom post type is "event").

    add_filter( 'pre_get_posts', 'my_get_posts' );
    
    function my_get_posts( $query ) {
    	if ( is_home() && $query->is_main_query() )
    		$query->set( 'post_type', array( 'post', 'event' ) );
    
    	return $query;
    }

    via Justin Tadlock

  3. scormeny
    Member
    Posted 2 years ago #

    Thank you very much to the tip, Felix! I will give it a try.

    I'm thinking this will work awesomely for me on the front page of the site and possibly in searches. Do you know how it affects taxonomies and category pages?

    At any rate I am excited to check it out. Take care.

Topic Closed

This topic has been closed to new replies.

About this Topic