Question

I've used pre_get_post to modify and display my custom post type in an archive page with pagination which uses my custom created archive template, see here. It it displaying the posts exactly what I need, however, it seems like it is not using my custom template. It is using the default template even though I already set a custom page template in my page settings. It is like this when in default template, and should be like this when using custom template.

Does everyone know how I can change the page template to my custom one when using pre_get_post?

I have tried the archive_videos_template() custom function that I found on this link, which you can see in this image but it is not working.

Please let me know if you have any idea how to do this because I wanted to modify the template of my custom post type in a page.

Thanks

Here's my initial code in function.php to display custom post type in archive page with pagination and the other function that I tried to change page template.

function post_type_videos( $query )
    {
      if ( ! is_admin() && is_post_type_archive( 'videos_cpt' ) && $query->is_main_query() )
      {
        $query->set( 'post_type', 'videos' ); //set query arg ( key, value )
        $query->set( 'posts_per_page', 2 ); //set query arg ( key, value )
        return $query;

        // custom page template
        add_filter( 'template_include', 'archive_videos_template', 99 );
      }
    }

    add_action( 'pre_get_posts' ,'post_type_videos', 1, 1 );

    function archive_videos_template( $template )
    {
        $target_tpl = 'archive-videos_cpt.php'; // EDIT to your needs

        remove_filter( 'template_include', 'archive_videos_template', 99 );

        $new_template = locate_template( array( $target_tpl ) );

        if ( ! empty( $new_template ) )
            $template = $new_template;;
    }

        return $template;
    }
Was it helpful?

Solution

You corrected the filter hook name from archive-videos to template_include, but in the first function return $query; statement comes before add_filter(), so the filter is not added.

add_action( 'pre_get_posts' ,'post_type_videos' );

function post_type_videos( $query )
{
    if ( ! is_admin() && $query->is_post_type_archive( 'videos_cpt' ) && $query->is_main_query() )
    {
        $query->set( 'post_type', 'videos' ); //set query arg ( key, value )
        $query->set( 'posts_per_page', 2 ); //set query arg ( key, value )

        // custom page template
        add_filter( 'template_include', 'archive_videos_template', 99 );

        return $query;
    }
}

function archive_videos_template( $template )
{
    remove_filter( 'template_include', 'archive_videos_template', 99 );

    $target_tpl = 'archive-videos_cpt.php';
    $new_template = locate_template( array( $target_tpl ) );
    if ( ! empty( $new_template ) )
        $template = $new_template;;
    }

    return $template;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top