Link to home
Start Free TrialLog in
Avatar of Alicia St Rose
Alicia St RoseFlag for United States of America

asked on

Need to keep 10 most recent post with term

Here's this old chestnut again.

I've got this to work: Most recent 10 posts have term "Free". Whenever a new post is added eleventh post gets term "Premium", the term with id 174 is excluded from this dynamic term naming.

/*Add terms to posts */

add_action( 'save_post', 'total_set_default_object_terms', 11, 2 );
function total_set_default_object_terms( $post_id, $post ) {
  if ( 'publish' === $post->post_status) {
    $position = 1;
    $args = [
      'numberposts' => -1,
      'order' 			=> 'DESC',
      'orderby' 		=> 'date',
      'post_type' 	=> 'post',
      //'taxonomy' => 'episode_type',
      'tax_query' => array(
					array(
					'taxonomy' => 'episode_type',
					'field' => 'id',
					'terms' => array( '174' ),
					'operator' => 'NOT IN',
					),
					),
						];
						
						
    $posts = get_posts( $args );// query posts that are episodes in descending publish date order
    foreach($posts as $post) {
      if ($position > 10) {
        wp_set_object_terms($post->ID, 'premium', 'episode_type', false);
      }
             
      $taxonomies = get_object_taxonomies( $post->post_type );
      
      $position++;
     }
  }
}

Open in new window


However, when I trash any of the 10 most recent posts and update any post, I don't get 10 Free posts, just the number remaining after the trashing/deletions.

User generated image
How do I remedy that in the code above?
Avatar of Alicia St Rose
Alicia St Rose
Flag of United States of America image

ASKER

Anyone?
A lot to digest - which is maybe why there are the sounds of silence on this one.

Can we go through this step by step so those of us not close to the project can onboard what is going on.

Why should it be at 10 in your diagram?
I'm wondering if I should reopen the previous question. I thought it was solved but when I trash posts, it throws things off.

So, for the sake of this thread:

My client wants the most 10 recent posts to be free to the public, and the rest, the archive, to be behind a paywall. I've got this working by giving all post that are older than the latest 10 the term "premium". This happens dynamically whenever a new post is published. I noticed however that when I trash one of the 10 most recent, I'm not able to remove the term from a post that now has the term "premium" so that i can have the last 10 post always free no matter what happens!

I'm wondering if I can somehow use a trashed trigger as well as published? Does that make any sense?

I guess I'm just finding out how to maintain the premium term on post 11 and onward no matter what happens to the last 10.
I'll check this out tonight or tomorrow!
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.