0
votes

I'm failing to add a custom post type to the query using pre_get_posts. I've created my own theme and integrated this plugin (https://github.com/Develop-With-WP/wp-job-listing).

How do I now add this new post_type 'job' to my query? I tried this:

function add_my_post_types_to_query( $query ) {
    if ( is_home() && $query->is_main_query() )
        $query->set( 'post_type', array( 'post', 'page', 'movie', 'job' ) );
    return $query;
}
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );

I am able to check the post_type but when I put [shortcode_debug] in the editors' post and call /general/first-blog/ .

But when I'm calling /job/sales/ it still doesn't render anything. What am I missing?

3

3 Answers

1
votes

Try this,

function add_my_post_types_to_query( $query ) {
    if ( $query->is_home() && $query->is_main_query() )
        $query->query_vars[ 'post_type' ] = [ 'post', 'page', 'movie', 'job' ];    
}
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
1
votes
add_action( 'pre_get_posts', 'get_posts_plus_cpt' );
function get_posts_plus_cpt( $query ) {
   if ( $query->is_home() && $query->is_main_query() ) {        
      $query->set( 'post_type', 'job' );
   }
}
0
votes
add_action( 'pre_get_posts', 'get_posts_plus_cpt' );

function get_posts_plus_cpt( $query ) {

    if ( $query->is_home() && $query->is_main_query() ) {        
       $query->set( 'post_type', 'job' );
    }
    return $query;
}