categories - Change post category within pre_get_posts

admin2025-06-04  1

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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749005210a315548.html

最新回复(0)