Skip to content

Instantly share code, notes, and snippets.

@EdenK
Last active December 26, 2017 10:02
Show Gist options
  • Save EdenK/1ec2e87048e801748db8feb3ef5ee4a1 to your computer and use it in GitHub Desktop.
Save EdenK/1ec2e87048e801748db8feb3ef5ee4a1 to your computer and use it in GitHub Desktop.
Wordpress: Change options-reading.php static page dropdown to custom post type
<?php
/**
* Change options-reading.php static page post type
*/
function add_cpt_to_static_home($output, $r, $pages) {
if($r['name'] === 'page_on_front' && !isset($r['post_type'])) {
$post_type = 'post'; // Set the post type
$r['post_type'] = $post_type;
$output = wp_dropdown_pages($r);
if(!$output) {
$output = sprintf(__('There is no posts in %s exists'), get_post_type_object($r['post_type'])->label);
}
}
return $output;
}
add_filter( 'wp_dropdown_pages', 'add_cpt_to_static_home', 10, 3 );
/**
* Set post type to the static homepage main query
*/
function set_cpt_to_home_main_query($query) {
if (!is_admin() && $query->is_main_query() && 'page' === get_option( 'show_on_front' )) {
$query_page_id = get_query_var( 'page_id' );
$front_page_id = get_option( 'page_on_front' );
if($query_page_id === $front_page_id) {
$post_type = get_post_type( $front_page_id );
$query->set( 'post_type', $post_type );
}
}
}
add_action( 'pre_get_posts', 'set_cpt_to_home_main_query', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment