filters – Using two posts_orderby add_filter makes conflict

Question

I’m trying to sort two cpt posts with term_order by using add_filter with posts_order hook. But it seems that the second filter override the first. How can i avoid this ? Do I have to create custom posts_orderby hooks ? Thanks for your help !

function ts_sort_package_in_categorie($orderby, $query)
{
    
    if (!ts_is_package_categorie_tax_query($query)) return;
    global $wpdb;
    return "{$wpdb->term_relationships}.term_order ASC";
    
}
add_filter('posts_orderby', 'ts_sort_package_in_categorie', 100, 2);

function ts_is_package_categorie_tax_query($query)
{
    if (empty($query->tax_query)) return;
    if (empty($query->tax_query->queries)) return;
    return in_array(
        $query->tax_query->queries[0]['taxonomy'],
        ['package_categorie'],
        true
    );
}

and :

function ts_sort_ticket_in_categorie($orderby, $query)
{
    
    if (!ts_is_ticket_categorie_tax_query($query)) return;
    global $wpdb;
    return "{$wpdb->term_relationships}.term_order ASC";
    
    
}
add_filter('posts_orderby', 'ts_sort_ticket_in_categorie', 99, 2);

function ts_is_ticket_categorie_tax_query($query)
{
    if (empty($query->tax_query)) return;
    if (empty($query->tax_query->queries)) return;
    return in_array(
        $query->tax_query->queries[0]['taxonomy'],
        ['ticket_categorie'],
        true
    );
}

I also tried something like this with no results :

function ts_sort_ticket_package_in_categorie($orderby, $query)
{
    if (!ts_is_package_categorie_tax_query($query)) return;
    global $wpdb;
    return "{$wpdb->term_relationships}.term_order ASC";
    
    
    if (!ts_is_ticket_categorie_tax_query($query)) return;
    global $wpdb;
    return "{$wpdb->term_relationships}.term_order ASC";
        
        
    }
add_filter('posts_orderby', 'ts_sort_ticket_package_in_categorie', 99, 2);

function ts_is_ticket_categorie_tax_query($query)
{
    if (empty($query->tax_query)) return;
    if (empty($query->tax_query->queries)) return;
    return in_array(
        $query->tax_query->queries[0]['taxonomy'],
        ['ticket_categorie'],
        true
    );
}
function ts_is_package_categorie_tax_query($query)
{
    if (empty($query->tax_query)) return;
    if (empty($query->tax_query->queries)) return;
    return in_array(
        $query->tax_query->queries[0]['taxonomy'],
        ['package_categorie'],
        true
    );
}

0
Paul Nouvel 2 months 2022-11-25T11:01:37-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse