Created
February 3, 2015 18:52
-
-
Save JulienMelissas/ffda551ca7b21aa6e7fb to your computer and use it in GitHub Desktop.
Sort Posts by Menu Order
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 | |
/* | |
Plugin Name: Sort Posts by Menu Order (SPBMO) | |
Description: This plugin adds support to posts or custom post types for menu order, and sorts queries for those post types by menu_order on the front end. It's reccommended that you download <a href="https://wordpress.org/plugins/simple-page-ordering/">Simple Page Ordering</a> if you'd like to display by menu_order in the admin and to add Drag/Drop support. | |
Plugin URI: http://julienmelissas.com/add-drag-drop-support-to-posts | |
Author: Julien Melissas | |
Author URI: http://julienmelissas.com | |
Version: 1.0 | |
*/ | |
// This function grabs the post types array and applies a filter for developer modification | |
function spbmo_get_spbmo_post_types() { | |
$post_types = apply_filters('spbmo_post_types', array('post')); | |
return $post_types; | |
} | |
// This function modifies the query for each post type in our option | |
function get_posts_by_menu_order( $query ) { | |
$post_types = spbmo_get_spbmo_post_types(); | |
// Check to make sure we're not in the admin panel or feed | |
if(!is_admin() && !is_feed() ) { | |
foreach ($post_types as $post_type) { | |
// Make sure we're in the main query | |
if (is_main_query($post_type)) { | |
$query->set( 'orderby', 'menu_order' ); | |
$query->set( 'order', 'ASC' ); | |
} | |
} | |
} | |
} | |
// Now force the post type to display based on menu_order | |
add_action( 'pre_get_posts', 'get_posts_by_menu_order' ); | |
// This function adds page-attribute support for each post type in our option | |
function spbmo_alter_post_types() { | |
$post_types = spbmo_get_spbmo_post_types(); | |
foreach ($post_types as $post_type) { | |
// First set up the post type so that they can be sorted by menu_order... | |
add_post_type_support( $post_type, 'page-attributes' ); | |
} | |
} | |
add_action('wp_loaded', 'spbmo_alter_post_types'); | |
// This is an example of how to add other post types | |
function spbmo_post_types_alter_value($option_value) { | |
$option_value = array( | |
'post', | |
'story' | |
); | |
return $option_value; | |
} | |
add_filter('spbmo_post_types', 'spbmo_post_types_alter_value'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment