Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm looking to use Custom Post Types to create a simplified process for key individuals to add information to a webpage. Let's use a Job Posting as an example, with custom fields for Job Title, Closing Date, etc.

However, I also have regular posts across a number of categories.

I would like to have all posts listed together. So for the home page where it shows the last 3 posts, that includes those in custom post types (there will be several).

I would also like to have these custom posts automatically set to specific categories, but these categories should be a part of the regular post types. If "Job Postings" and "Press Releases" are two categories under my regular post menu, I would also like a CPT of "Job_Posting" to be set to "Job Postings" automatically.

This is for situations where we need to post information that would belong to a job posting category but wouldn't necessarily be an actual postings with the required fields set up in that CPT.

Am I going about this the wrong way entirely?


Update:

I've changed my WP_Query to the following, and it still won't show CPT:

<ul class="home-content-recent">
            <?php 
            $args = array(
                'post_type' => 'any',
            );
            $recent = new WP_Query($args);
            $recent->query('showposts=3');
            if($recent->have_posts()) : while($recent->have_posts()): $recent->the_post();?>
        <li>
        <a class="recent-title" href="<?php the_permalink()?>"><?php the_title()?></a>
        <div class="post-meta-data">
        <p class="recent-time"><?php the_time('l, F j, Y')?></p>
        <p class="recent-category"><?php the_category(', '); ?></p>
        <p class="recent-commentcount"><?php comments_number( 'No Comments', '1 Comment', '% Comments' ); ?></p>
        </div>
        <div class="recent-excerpt"><?php the_excerpt()?></div>
        </li>
            <?php endwhile ?>
            <?php else : ?>
            <?php endif ?>
</ul>
share|improve this question
add comment

1 Answer 1

For the first part of your question, a pre_get_posts hook is all you would need. This code should work, providing you are using the default "Latest Posts" setting as your front page (Settings > Reading).

add_action( 'pre_get_posts', 'se132019_home_page_all_post_types' );
function se132019_home_page_all_post_types( $query ) {
    if ( ! is_admin() $query->is_main_query() && $query->is_posts_page() ) {
        $query->set( 'post_type', 'any' );
    }
}

If you are using a custom WP_Query, just add 'post_type' => 'any' as an argument.

The second piece is a personal preference for you - you could just house all posts in Posts, and display them differently depending on the terms (categories) they are attached to. Otherwise, you would need to do the following 2 things:

  1. Add your custom post types (Job Listings) to the category taxonomy. This can be done when registering the post type using taxonomies argument, or using register_taxonomy_for_object_type.
  2. hook into the save_post method, check to see if the post was attached to any Categories. If not, manually assign it to the proper category, depending on which post type it was.

EDIT::

your first couple lines should look like the following. When a WP_Query object is created, it automatically calls the query() function. You are calling it a second time, passing it new arguments.

        <?php 
        $args = array(
            'post_type' => 'any',
            'posts_per_page' => 3
        );
        $recent = new WP_Query($args);
share|improve this answer
    
Hi Eric, First, I've added your suggestion to the WP_Query and it isn't working. I've added some more info to my question above. –  Nathan S Jan 31 at 20:23
    
Second, it sounds like maybe CPT isn't what I want? Is it possible to display a post (and custom fields) in a specific way, depending on what category they are posted in? This would simply things quite a bit, I think. –  Nathan S Jan 31 at 20:26
    
Whoops, misread your answer that I wouldn't need the pre_get_posts if I'm using a custom WP_Query. Where exactly do I add that code? I'm getting errors from my functions file.. –  Nathan S Jan 31 at 20:40
    
You only need that one line then. Look in my answer at the Edit - it tells you why you're not seeing all post types. –  Eric Holmes Jan 31 at 20:57
    
Perfect, thanks! Now, how do I get it to exclude 'pages'? It's showing all posts along with my regular pages as posts, too. Edit: I went with naming the CPT and posts together in that array, but is there a way to say "Any but not Pages" ? That would be better, I think, to future-proof. –  Nathan S Jan 31 at 22:43
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.