Documentation - How To - Programmatically retrieve the sort_id and sort_view_id by specified/query arguments

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

Programmatically retrieve the sort_id and sort_view_id by specified/query arguments

Within the Dashboard, multiple sorts can be created to match different areas of your site. This provides a powerful and flexible way to control sorting for virtually any section of a website.In certain situations, the sort_id or sort_view_id must be known at the code level before further processing can take place.

Advanced Post Types Order provides multiple helper methods that allow developers to programmatically determine which sorting configuration applies to a specific set of query arguments. These methods are especially useful when working at code level, where knowing the active sort_id or sort_view_id is required before performing additional logic or processing.

The methods analyze the provided query arguments and return the matching sort data that would be applied by APTO for that query.

get_sort_view_id_by_arguments( $query_args, $sort_args )

Returns the sort_view_id that matches the given query arguments.

The sort_view_id represents a specific sort view configuration, which include contextual rules such as post type, taxonomy filters, or other query constraints. This method is helpful when you need to identify the exact sort view that applies to a query.

Parameters

    • $query_args (array) Mandatory
      A standard WordPress query arguments array (similar to WP_Query arguments).
    • $sort_args (array) Optional
      A sort settings arguments array.

Return value

    • (int|false)
      The matching sort_view_id if found, or false if no matching sort view applies.

get_sort_id_by_arguments( $query_args, $sort_args )

Returns the sort_id that matches the given query arguments.

The sort_id represents the main sort entity associated with one or more sort views. This method is useful when you only need the parent sort identifier, regardless of the specific view that applies.

Parameters

    • $query_args (array) Mandatory
      A standard WordPress query arguments array.
    • $sort_args (array) Optional
      A sort settings arguments array.

Return value

  • (int|false)
    The matching sort_id if found, or false if no matching sort applies.

 

In this example, the methods evaluate the query arguments for WooCommerce products filtered by a specific product category and return the corresponding sort_view_id and sort_id that Advanced Post Types Order would apply for this query.

    $query_args = array(
                    'post_type'                 => 'product',
                    'tax_query'                 => array(
                                                            array(
                                                                'taxonomy'      => 'product_cat',
                                                                'field'         => 'term_id',
                                                                'terms'         => 40
                                                                )
                                                            )
                    );
                    
    $sort_view_id        =   APTO_functions::get_sort_view_id_by_arguments( $query_args );                
    $sort_id   =   APTO_functions::get_sort_id_by_arguments ( $query_args );

 

 

query_match_sort_id( $query, $args )

The query_match_sort_id() method provides an alternative way to identify the sort_view_id that applies to a specific query by evaluating a fully parsed WP_Query object.

Unlike argument-based helpers, this method operates directly on the query object and is useful in scenarios where the query has already been built or modified, or when additional matching rules (such as autosort conditions) must be taken into account.

The method returns the matching sort view ID, from which the parent sort_id can be retrieved if needed.

Parameters

  • $query (WP_Query) Mandatory
    A parsed WP_Query object containing the query variables to be evaluated.
  • $sort_args (array) Optional
    Optional matching arguments that further refine the sort selection (e.g. autosort rules).

Return value

  • (int|false)
    The matching sort_view_id if found, or false if no applicable sort view is identified.

 

This approach is particularly useful when working with existing query objects or when Advanced Post Types Order needs to resolve sorting based on runtime query conditions rather than static argument matching.

global $APTO;
 
$sortID =   FALSE;
 
$args   =   array(
                    '_autosort' =>  array('yes')  
                    );
                     
$sort_view_id   =   $APTO->functions->query_match_sort_id( $wp_query, $args );
 
if($sort_view_id    >   0)
    {
        $sort_view_post     =   get_post($sort_view_id);
        $sortID             =   $sort_view_post->post_parent;
    }