Skip to content

Instantly share code, notes, and snippets.

@ibrokemywp
ibrokemywp / do-sort-like-this.php
Last active August 29, 2015 14:09
Sort your custom post type via pre_get_posts like this
/**
* Check before sorting your custom post type admin lists so you don't
* override other changes.
*/
add_action( 'pre_get_posts', array( $this, 'admin_order_posts' ) );
public function admin_order_posts( $query ) {
if( ( is_admin() && $query->is_admin ) && $query->get( 'post_type' ) == 'my_custom_post_type' ) {
// This prevents other orderby options from breaking.
@ibrokemywp
ibrokemywp / dont-sort-like-this.php
Last active August 29, 2015 14:09
Don't sort your custom post type via pre_get_posts like this
/**
* Don't sort your custom post type admin lists this way because it will
* override other sorting being executed on this list.
*/
add_action( 'pre_get_posts', array( $this, 'admin_order_posts' ) );
public function admin_order_posts( $query ) {
if( ( is_admin() && $query->is_admin ) && $query->get( 'post_type' ) == 'my_custom_post_type' ) {
$query->set( 'orderby', 'menu_order' );
$query->set( 'order', 'ASC' );
@ibrokemywp
ibrokemywp / textdomain-back-compat.php
Created November 16, 2014 13:25
Load a .mo file for the old textdomain if one exists
/**
* Load a .mo file for the old textdomain if one exists
*
* If you have to change your textdomain to comply with upcoming
* plugin and theme repository standards, this function will check to
* see if an old translation file exists and load it if it does, so
* that people don't lose their translations.
*
* h/t: https://github.com/10up/grunt-wp-plugin/issues/21#issuecomment-62003284
*/
@ibrokemywp
ibrokemywp / nested_filters.php
Created November 14, 2014 01:05
Applying the_content filter inside the_content filter causes lots of tears
/**
* I'm going to add some content from one post to
* the content of another post. Yay for me.
*/
add_filter( 'the_content', 'my_little_content_addition' );
function my_little_content_addition( $content ) {
if ( is_the_right_content() ) {
/**
@ibrokemywp
ibrokemywp / get_current_screen().php
Last active August 29, 2015 14:09
get_current_screen() returns null in ajax requests
/**
* Let's say you're parsing query args in the admin so
* that you can apply some custom filters for your
* custom post type list table.
*/
add_filter( 'parse_query', 'filter_my_post_type' );
function filter_my_post_type( $query ) {
/**
* Oh good, you've put in that is_admin() check