Last active
August 2, 2018 22:18
-
-
Save nico-martin/2d6648ce90dd5ccbc45f608bf4955b80 to your computer and use it in GitHub Desktop.
Set post as WP Front-Page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'get_pages', 'add_posts_to_frontpage_dropdown', 20, 2 ); | |
add_action( 'pre_get_posts', 'update_frontpage_post_query' ); | |
function add_posts_to_frontpage_dropdown( $pages, $r ) { | |
if ( array_key_exists( 'name', $r ) && 'page_on_front' == $r['name'] ) { | |
$args = [ | |
'post_type' => 'post', | |
'posts_per_page' => - 1, | |
'post_status' => 'publish', | |
]; | |
$items = get_posts( $args ); | |
$pages = array_merge( $pages, $items ); | |
} | |
return $pages; | |
} | |
function update_frontpage_post_query( $query ) { | |
if ( ! $query->is_home() || ! $query->is_main_query() ) { | |
return; | |
} | |
// get_option() does not work because of the built in filters | |
global $wpdb; | |
$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", 'page_on_front' ) ); | |
if ( ! is_object( $row ) ) { | |
return; | |
} | |
$args = "p={$row->option_value}"; | |
$query->init(); | |
$query->parse_query( $args ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment