Ask your WordPress questions! Pay money and get answers fast! (more info)

Checkbox to add Custom Post Type to Main WordPress Loop & Feed WordPress

  • SOLVED

I have a problem with WordPress Custom Post Types that I can't seem to wrap my head around.

I would like to have a category or a custom taxonomy checkbox on my custom post type, and if this one is checked, then display the post in the WordPress main loop and in the WordPress feed.

Note: I don't want to display all posts from my custom post type in the main loop and feed, only the ones that are checked.

I want it to be sorted the same way as all other posts, by date, so it shouldn't be a featured post or anything like that, it should behave just like the other, "normal", posts.

Is this even possible to achieve? I've tried to google for a solution, but with no luck so far.

Thanks!

// Jens.

Answers (1)

2014-12-21

Kyle answers:

Start by adding the post type to the primary query, you may also want to add a conditional based on the template you are using:

add_filter( 'pre_get_posts', 'add_my_cpt' );
function add_my_cpt( $query ) {

if( is_home() ){ //check if it is the posts page, remove if you want this sitewide

$query->set( 'post_type', array( 'post', 'my-custom-post-type' ) );

return $query;

}

}


The second part of this is a little strange because you are actually removing some of those posts you just added. So we will add a second line to the query above that grabs posts that don't have that checkbox selected and add a post__not_in parameter to remove it, give me a minute to write that.


Kyle comments:

It actually may be easier to make a category to NOT include because of the bahavior of the posts__in and posts_not_in parameters which you can use to make it neater. This will REMOVE all posts of a certain category from the loop,

add_filter( 'pre_get_posts', 'add_my_cpt' );
function add_my_cpt( $query ) {

if( is_home() ){ //check if it is the posts page, remove if you want this sitewide

$args = array(
'fields' => 'ID',
'post_type' => 'my-custom-post-type',
'cat' => '4'//category ID to remove
);

$remove_these = get_posts( $args );

$query->set( 'post_type', array( 'post', 'my-custom-post-type' ) );
$query->set( 'posts_not_in', $remove_these );

return $query;

}

}


Jens Filipsson comments:

Hi Kyle,
thanks a lot! Haven't had the time to try the code yet, but it looks promising! :)

How about the feeds, don't I need a separate function for this?

// Jens.


Kyle comments:

Yes, it should still work, but will need to be tested. We may need to change to the 'request' hook and tweak the above slightly, let me know when you try it out and we can go from there.


Jens Filipsson comments:

Hey Kyle, sorry for my late response, haven't been able to solve this yet. At the moment I have this code, but doesn´t seem to work unfortunately. At the moment all posts and custom post types shows up:


if ( ( is_front_page() && $query->is_main_query() ) || is_feed() ) {

$args = array(
'fields' => 'id',
'post_type' => 'usp_post',
'tax_query' => array(
array(
'taxonomy' => 'firstpage',
'field' => 'slug',
'terms' => array('featured')
)
)

);



$remove_these = get_posts( $args );



$query->set( 'post_type', array( 'post', 'usp_post' ) );

$query->set( 'posts_not_in', $remove_these );

}