最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - Change post category within pre_get_posts

matteradmin10PV0评论

I have written a filter to be called in pre_get_posts to remove posts from a category page based on a meta_key (EventDate which is in the form YYYYMMDD), which is working.

However I'm struggling with the next step, which is to change the category of the now excluded post. Does anyone have any suggestions on how to get this working?

I have tried:

function pre_get_posts_filter($query){
    if($query->is_main_query()){
        if(is_archive()):
            if(is_category($category='Events')):
                $a=array();
                if(get_post_meta(get_the_ID(),$key='EventDate',$single=true)<date('Ymd')):
                    $query->set('post__not_in', array_push($a,get_the_ID()));
                    wp_set_post_categories(get_the_ID(),array('PastEvents'));
                 else:
                   //Set the order ASC or DESC
                   $query->set( 'order', 'ASC' );
                   //Set the orderby
                   $query->set( 'orderby', 'meta_value_num');
                   $query->set('meta_key', 'EventDate');
                endif;
            endif;
        endif; 
        return $query;
    }
    };

Any help would be greatly appreciated. Thanks

Post a comment

comment list (0)

  1. No comments so far