Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/*
Plugin Name: MEM Category Order
Plugin URI: https://gist.github.com/
Description: Functionality plugin for https://wordpress.org/support/topic/ordering-post/.
Version: 0.1
Author: Manuel Schmalstieg
Author URI: https://ms-studio.net
*/
/**
* This sample code is written in response to:
*
* https://wordpress.org/support/topic/ordering-post/
*
* "i would like to use your plugin but is there a way to order all the post of a specific category by the event start date?"
*
* This example will order posts of the category with slug "events" by their start date.
*
* Caveat: this Category Archive will now only show articles that have a start date.
*
*/
function mem_category_order( $query ) {
if ( $query->is_category('events') ) {
$query->set( 'meta_key', '_mem_start_date');
$query->set( 'orderby', 'meta_value');
}
}
add_filter( 'pre_get_posts', 'mem_category_order' );
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.