Home > Mobile >  Showing specific taxonomy term always at the end
Showing specific taxonomy term always at the end

Time:08-24

I have a custom post type called Jobs, and it includes also custom taxonomy work-category. So far I was displaying my content in the following way (that works):

<div >
    <?php
    $terms = get_terms([
    'taxonomy' => 'work-category',
    'hide_empty' => true,
    'orderby'  => 'count',
    'order' => 'DESC',
    ]);


    if ($terms) {
    foreach ($terms as $category) { ?>

        <h2 >
            <?= $category->name ?>
        </h2>


        <?php
        $posts = get_posts(array(
            'posts_per_page'    => -1,
            'post_type'         => 'jobs',
            'tax_query' => [
            [
                'taxonomy' => 'work-category',
                'field' => 'term_id',
                'terms' => $category->term_id,
            ]
            ]
        ));

        if ( $posts ) {
            foreach( $posts as $post ) {
            setup_postdata( $post );
            include 'WorkView.php';
            };
            wp_reset_postdata();
        }

    }
    } else { //no categories

    $posts = get_posts(array(
        'posts_per_page'    => -1,
        'post_type'         => 'career'
    ));

    if ( $posts ) {
        foreach( $posts as $post ) {
        setup_postdata( $post );
        include 'JobListThumb.php';
        };
        wp_reset_postdata();
    }

    }
    ?>

</div>

I've decided to add one more featured called "show always at the end". And for that I have created a custom field (name: stick_to_end), dedicated only to terms from this specific taxonomy (work-category). So simply what I'm trying to achieve is to show all works categories and order them by count BUT on still show specific one at the very end (so sth like sticky posts/category but the other way around). What I've already tried was:

1. An array in orderby so `'orderby'  => array ('count' => DESC, 'stick_to_end' => ASC)`

2. meta_query
<code>
    'meta_query' => array(
                'relation' => 'AND',
                array(
                    'key'      => 'count',
                    'value'    => EXISTS, 
                ),
                array(
                    'key'      => 'stick_to_end',
                    'compare'  => '=',
                    'value'    => false, 
                ),
            )

But this also doesn't work. How to achieve that or where I'm doing the mistake?

CodePudding user response:

ifThe get_terms you're using, use WP_Term_Query internally which doesn't support ordering by multiple keys the same way WP_Query does.

So here's a custom method to order them after you've queried them.

usort( $terms, function( $a, $b ) {
    $a_ste = (int) get_term_meta( $a->term_id, 'stick_to_end', true );
    $b_ste = (int) get_term_meta( $b->term_id, 'stick_to_end', true );

    if ($a_ste == $b_ste) return 0;
    return ($a_ste < $b_ste) ? -1 : 1;
} );

Place this code before if ($terms) {

Let me know if this worked for you.

  • Related