Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.