Decrement term in for each

So I am catching custom posts in wordpress by for eaching my custom terms. To get a post from the next term in the loop I use $terms[++$key]->name and it all works fine. However when I want to do the opposite: $terms[--$key]->name somehow it stays within the same term (even though there is a previous term). Any ideas? In this code the intention is to get the post title of the last post of the previous term.

$args = array
    (
        'post_type' = 'projects',
        'post_status' = 'publish',
        'posts_per_page' = 1,
        'tax_query' = array(
                array
                (
                    'taxonomy' = 'mycategory',
                    'field' = 'slug',
                    'terms' = strtolower($terms[--$key]-name)
                )
            ),
        'orderby' = 'menu_order',
        'order' = 'DESC'
    );
    $the_query = new WP_Query($args);   
    if($the_query-have_posts() ) : while ( $the_query-have_posts() ) : $the_query-the_post();

        $prev_term_post = $post-post_title;

    endwhile;
    endif;

This is all within the for each loop for terms:

?php foreach($terms as $key = $term) : ?

The weird part is the ++ does work. So it looks like it skips the decrement.

Just checked, when outputing $terms[--$key]->name it does indeed give the previous term, just not the last post in that term, instead the last post of the current term.

Topic previous terms get-posts custom-taxonomy custom-post-types Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.