Query Filters 1.0.1: Custom Sort Options via PHP

Home / Resources / Query Filters 1.0.1: Custom Sort Options via PHP
Groundworx Query Filters

The Filter Orderby block now supports custom sort options registered through PHP. Sort by custom fields, combine multiple sort criteria, or use callbacks for complex sorting logic.

What’s new

Register custom orderby options using the groundworx/filter_orderby_options-{post_type} filter. Options are post-type specific and appear automatically in the block editor.

Sort by a custom field:

add_filter( 'groundworx/filter_orderby_options-product', function( $options ) {
    $options['by-price'] = array(
        'orderby'  => 'meta_value_num',
        'meta_key' => 'price',
        'order'    => 'ASC',
    );
    return $options;
} );

Combine multiple sort fields — featured items first, then by price, then alphabetically:

add_filter( 'groundworx/filter_orderby_options-product', function( $options ) {
    $options['featured-first'] = array(
        'meta_query' => array(
            'featured_clause' => array(
                'key'  => 'is_featured',
                'type' => 'NUMERIC',
            ),
            'price_clause' => array(
                'key'  => 'price',
                'type' => 'NUMERIC',
            ),
        ),
        'orderby' => array(
            'featured_clause' => 'DESC',
            'price_clause'    => 'ASC',
            'title'           => 'ASC',
        ),
    );
    return $options;
} );

Use callbacks for complex logic like “trending this week”:

add_filter( 'groundworx/filter_orderby_options-post', function( $options ) {
    $options['trending'] = array(
        'callback' => function( $query ) {
            $query->set( 'meta_key', 'view_count' );
            $query->set( 'orderby', 'meta_value_num' );
            $query->set( 'order', 'DESC' );
            $query->set( 'date_query', array(
                array( 'after' => '1 week ago' ),
            ) );
        },
    );
    return $options;
} );

Any valid WP_Query parameter works.

Also in this release

  • Filter Orderby now auto-fixes Query Loop’s orderBy when switching to a post type that doesn’t support the current sort (e.g., resets menu_order to date for posts)
  • Fixed defaultOrderBy reset logic to validate independently when post type changes

Full documentation in the Filters tutorial.

Get Query Filters

Available standalone or as part of the Groundworx Core bundle.

Let’s solve what’s holding you back.

Ready to Build Better WordPress Sites?

Join agencies and freelancers who’ve stopped fighting with page builders