Today’s lesson — WP_Query

I was trying to get a list of person-type posts (person being a custom post type). I had a chunk of code that looked a little like this:


$args = array( 'post_type'  => Class::POST_TYPE, 'fields'     => 'ids', 'nopaging'   => true, 'orderby'    => array( 'menu_order', 'title' ), 'order' => 'ASC', 'numberposts' => -1, ); $query = new WP_Query( $args );

But it just dumped them out using post_date ordering instead, like it was defying me. I couldn’t figure it out. I hammered away at it, trying different things. 'orderby' => array( 'menu_order' ) didn’t work, but 'orderby' => 'rand' did. I think I somehow managed to skip over trying 'orderby' => 'menu_order' — I suspect, in hindsight, that might have worked.

In the end I resorted to reading the documentation. Here’s the code snippet that worked:


$args = array( 'post_type' => Class::POST_TYPE, 'fields' => 'ids', 'nopaging' => true, 'orderby' => array( 'menu_order' => 'ASC', 'title' => 'ASC', ), 'numberposts' => -1, ); $query = new WP_Query( $args );

Apparently this new, more powerful orderby clause is new in WordPress version 4.0 and up. I like it, it works for me. Now that I’ve figured it out, anwyays…

Tribe Events filters & actions

So I’m just starting to outline a developers’ tutorial on the filters & action hooks in Modern Tribe’s fantastic The Events Calendar plugin for WordPress, and I thought, Hmmm, I wonder how many filters and hooks there are in the code. So, being a nerd, I ran the following commands1 on my little Ubuntu machine:

% wget http://downloads.wordpress.org/plugin/the-events-calendar.3.0.3.zip
% unzip the-events-calendar.3.0.3.zip
% cd the-events-calendar
% find . -type f -exec grep apply_filters {} \; > filter-count.txt
% find . -type f -exec grep do_action {} \; >> filter-count.txt
% vi filter-count.txt

    :%s/.*apply_filters( '\([^']*\).*/\1/g
    :%s/.*do_action( '\([^']*\).*/\1/g
    :v/tribe_/d
    :wq

% sort -u filter-count.txt > unique-filters.txt
% wc -l unique-filters.txt

And what do you, a number popped out. According to my (very) rough guesstimation, there are 371 unique filters & hooks2 in The Events Calendar. Seems I have my work cut out for me.


  1. If you can think of a better way, I’d like to hear it. 
  2. Well, that start with tribe_, anyways.