Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nico-martin
Last active August 2, 2018 22:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nico-martin/2d6648ce90dd5ccbc45f608bf4955b80 to your computer and use it in GitHub Desktop.
Save nico-martin/2d6648ce90dd5ccbc45f608bf4955b80 to your computer and use it in GitHub Desktop.
Set post as WP Front-Page
<?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