Create sorting for queries containing Custom Field Handling / Meta

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPrint this page

Staring version 3.8.9 the Advanced Post Types Order got a major code extension to include the possibility to focus a sort on an exact front site query contain a Custom Field Handling / meta_query section. The Custom Fields meta data is the way WordPress save additional information for custom post types. These are used by custom code (theme and plugins) for post-processing.

A sample example on how a query including custom fields looks like:


$argv   =   array(
                        'post_type'     =>  "products",
                        'meta_query'    => array(
                                                'relation' => 'AND',
                                                array(
                                                    'key'     => "_color",
                                                    'value'   => array("blue", "red"),
                                                    'compare' => 'IN',
                                                    'type'      =>  "CHAR",
                                                ),
                                                array(
                                                    'key'     => "_availability",
                                                    'value'   => "yes",
                                            
                                                ),
                                            ),
                        );
                        
    $custom_query       =   new WP_Query($argv);

To create a sort for such custom query, the Sort Settings – Query Rules – Meta area can be used. It just require to create the entries as they appear in the query arguments:

advanced-post-types-order-sort-settings-query-meta-area

Once saved, the sortable interface will show only the items which match the set-up query, the ones which also display on front side. Drag and drop items around will re-order the objects on front side accordingly.