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;
}