Skip to content

Instantly share code, notes, and snippets.

@Zodiac1978
Last active November 4, 2021 22:29
Show Gist options
  • Save Zodiac1978/ced81b9a46dcd55d2701829deec912b7 to your computer and use it in GitHub Desktop.
Save Zodiac1978/ced81b9a46dcd55d2701829deec912b7 to your computer and use it in GitHub Desktop.
Display Wordpress Home/Front Page at top of Admin Pages List
<?php
/**
* Plugin Name: Page List Optimizer
* Description: Push Front Page & Posts Page & Privacy Page to top of admin list
* Plugin URI: https://torstenlandsiedel.de
* Version: 1.0
* Author: Torsten Landsiedel
* Author URI: http://torstenlandsiedel.de
* Licence: GPL 2
* License URI: http://opensource.org/licenses/GPL-2.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Push Front Page & Posts Page & Privacy Page to top of admin list
*
* @link https://wordpress.stackexchange.com/questions/188406/wordpress-home-front-page-display-at-top-of-admin-pages-list/279836#279836
* @param string $orderby The ORDER BY clause of the query.
* @param WP_Query $query The WP_Query instance (passed by reference).
* @return string $orderby The modified ORDER BY clause of the query with added IDs.
*/
function ezific_admin_static_pages_first( $orderby, $query ) {
// Leave if not Admin edit Pages query.
if ( ! ( is_admin() && $query->is_main_query() && 'page' === $query->get( 'post_type' ) ) ) {
return $orderby;
}
// Leave if neither static pages are set.
$front = (int) get_option( 'page_on_front' );
$posts = (int) get_option( 'page_for_posts' );
$privacy = (int) get_option( 'wp_page_for_privacy_policy' );
$ids = implode( ',', array_filter( array( $privacy, $posts, $front ) ) );
if ( empty( $ids ) ) {
return $orderby;
}
// Update the query.
global $wpdb;
$orderby = 'FIELD(' . $wpdb->posts . '.ID,' . $ids . ') DESC, ' . $orderby;
// Return the modified ORDER BY clause.
return $orderby;
}
add_filter( 'posts_orderby', 'ezific_admin_static_pages_first', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment