Skip to content

Instantly share code, notes, and snippets.

@DanielFloeter
Last active January 30, 2022 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DanielFloeter/f50bbf0ee9433c68bcfa1d5ca3ddcf3d to your computer and use it in GitHub Desktop.
Save DanielFloeter/f50bbf0ee9433c68bcfa1d5ca3ddcf3d to your computer and use it in GitHub Desktop.
Sort-by add-on
<?php
/*
Plugin Name: Order-by Add-on
Plugin URI: http://tiptoppress.com/downloads/term-and-category-based-posts-widget/
Description: Extension to add more sort possibilities for the premium widget Term and Category Based Posts Widget.
Author: TipTopPress
Version: 1.2
Author URI: http://tiptoppress.com
Installation:
- Download the file and rename it to Order-by-Add-on.php
- Upload it to the plugins folder [your-wordpress-installation]/wp-content/plugins/Order-by-Add-on.php
- Go to WordPress > Admin > PlugIns and activate the Extension
- Now, there is a new sort by: 'Name' visible
*/
namespace termCategoryPostsPro\OrderByAddOn;
// Don't call the file directly
if ( !defined( 'ABSPATH' ) ) exit;
/**
* Applied to the list of wp_query args.
*
* @return array of sort orders
*
* @since 0.1
*/
function query_args ( $query_args, $that, $instance ) {
// build own querys or override used query args
if ( $query_args['orderby'] == 'name' || $query_args['orderby'] == 'title' || $query_args['orderby'] == 'latest_asc' ) {
$query_args['order'] = 'ASC';
} else if ( $query_args['orderby'] == 'tour_date' ) {
$query_args['orderby'] = 'meta_value';
$query_args['meta_key'] = 'tour_date';
$query_args['meta_type'] = 'DATE';
$query_args['order'] = 'DESC';
}
return $query_args;
}
add_filter( 'cpwp_query_args', __NAMESPACE__.'\query_args', 10, 3 );
/**
* Applied to the list of sort orders for sort_by some new ones.
*
* @return array of sort orders
*
* @since 0.1
*/
function possible_sorts ( $sorts, $that, $instance ) {
// key gets 'sort_by', value gets name in the dropdown UI
$byName = array(
'latest_asc' => 'Latest (ASC)',
'tour_date' => 'Tour Date',
'name' => 'Name (Slug)',
'title' => 'Title');
$sorts = array_merge ($byName, $sorts);
return $sorts;
}
add_filter( 'cpwp_possible_sorts', __NAMESPACE__.'\possible_sorts', 10, 3 );
@RandalOulton
Copy link

" Now, there is a new sort by: 'Name' visible" Where do we look for this please?

@DanielFloeter
Copy link
Author

Can you give me a screenshot or a bit more details?

@RandalOulton
Copy link

Hi Daniel, I figured it out. Thanks!

For those in the future who are curious, this new "name" option will appear in the widget under the "Filter" section, as a choice in the "Show" field, (which might perhaps? be better named the "Sort" field.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment