Search code examples
phpwordpresscustom-post-type

WordPress Failing to add custom post type to query using pre_get_posts


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?


Solution

  • 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' );