Skip to content

Instantly share code, notes, and snippets.

@hirejordansmith
Created July 20, 2017 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirejordansmith/9a9a7fc72dbdb714c9608614c6213c61 to your computer and use it in GitHub Desktop.
Save hirejordansmith/9a9a7fc72dbdb714c9608614c6213c61 to your computer and use it in GitHub Desktop.
How to change which page template is set as default template for WordPress
<?php
add_action('add_meta_boxes', 'hjs_default_page_template', 1);
function hjs_default_page_template() {
global $post;
if ( 'post' == $post->post_type
&& 0 != count( get_page_templates( $post ) )
&& get_option( 'page_for_posts' ) != $post->ID // Not the page for listing posts
&& '' == $post->page_template // Only when page_template is not set
) {
$post->page_template = "listing-template.php";
}
}
@peakbecker
Copy link

Thanks for the code!
2 questions:

  • how does this code get executed?
  • which instances of 'post' should be replace with 'page'?
    Sorry, not a WP developer :-)

@catapultcc
Copy link

I think this function isen't working anymore with the new WP version.
Can't get it working on my websites

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment