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;
}
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;
}
Share
Improve this question
edited Jun 23, 2020 at 12:54
Jim
asked Jun 23, 2020 at 11:01
JimJim
134 bronze badges
6
|
Show 1 more comment
1 Answer
Reset to default 1You 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;
}
archive-video_cpt.php
? There's a screenshot of the block editor in your question but I don't understand why it's there. – Tom J Nowell ♦ Commented Jun 23, 2020 at 12:12add_filter( 'archive-videos', 'archive_video_template', 99 )
but there is no such filter. To filter the path of template usetemplate_include
filter hook, so change this line toadd_filter( 'template_include', 'archive_video_template', 99 )
. You can also try with thearchive_template
filter. – nmr Commented Jun 23, 2020 at 12:37