1

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;
    }
6
  • 1
    Hello! Your code is in an image, please edit your question using the edit link to put your code in a code block, not a screenshot. In order to debug this I would have to type it all out manually, and I don't have the time to do that. Also can you clarify what you're trying to do that this solves? Why couldn't you use 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:12
  • oh sorry. I edited my post and pasted the code. thank you for the reply
    – Jim
    Commented Jun 23, 2020 at 12:20
  • the screenshot of block editor shows the template that i used in that page which is the archive-video_cpt.php that I custom created, however, it is not being used when I try to display posts using pre_get_post. It is using the default template. I am not sure why
    – Jim
    Commented Jun 23, 2020 at 12:32
  • You have in code add_filter( 'archive-videos', 'archive_video_template', 99 ) but there is no such filter. To filter the path of template use template_include filter hook, so change this line to add_filter( 'template_include', 'archive_video_template', 99 ). You can also try with the archive_template filter.
    – nmr
    Commented Jun 23, 2020 at 12:37
  • oh, thanks, I'll try an get back to you
    – Jim
    Commented Jun 23, 2020 at 12:45

1 Answer 1

1

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;
}
1
  • Wooah. thank you for this adjustment. To be honest I am not an expert in wordpress so most of the time I am wondering what is this for what is that for. Thank you for this answer. works perfect, like what I wanted.
    – Jim
    Commented Jun 23, 2020 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.