Skip Navigation

[Resolved] Mark last 100 posts as NEW

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here it that the user wanted to mark the latest 100 posts when they are displayed in a view.

Solution:

The solution to this can be seen below.
https://toolset.com/forums/topic/mark-last-100-posts-as-new/#post-1201280

This support ticket is created 5 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 12 replies, has 2 voices.

Last updated by michaelM-30 5 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#1197110

I want to "mark" the last 100 posts with a transparent .png "NEW".
I cannot use the date of the last posts, as i use older dates for my post entries.
So I would have to use the post ID for this.

I can overlay the posts with a transparent png in the view like this (with additional .css of course):

<div class="pngcontainer1">
[wpv-post-featured-image size="medium"]
<img src="hidden link" />
</div>
<div id="Auszug" style="background-color: #FFFFFF;padding-left: 10px">
<p style="font-size:19px; color:#cc2828"> [wpv-post-type show="plural"] </p>
<h5>[wpv-post-link]</h5>
[wpv-post-date]
....

But this would mark ALL the posts as new - of course.
Is it possible to query for the last 100 posts with a javascript within the view? (or in the loop?)
And if so, can you help me?

Thanks
Michael

#1197361

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Michael,

Thank you for contacting our support forum.

Unfortunately at this time it isn't possible to do this since we are not able to create a loop counter. However I know that in our patch next week we would be getting a loop counter.

This way you can use our conditional shortcode to check which count you are at in the loop and add your overlay. I would highly advise waiting on this update as it would make this much easier.

Right now a custom solution would be needed to keep track of the current loop count.

Please let me know if this helps.

Thanks,
Shane

#1197471

Hi Shane,
that sounds great - can you give me a little headstart on what the
code will look like for my problem, when the new function is implemented?

Thanks
Michael

#1197509

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Michael,

Sure on our internal system i see that the shortcode is [wpv-loop-index]

Given there are no change to to this the shortcode would be.

The code below would be for all the items with index less than or equal to 100.


[wpv-conditional if="( '[wpv-loop-index]' lte '100' )"] 
<div class="pngcontainer1">
<a href="[wpv-post-url]">[wpv-post-featured-image size="medium"]</a>
<img src="<em><u>hidden link</u></em>" />
</div>
<div id="Auszug" style="background-color: #FFFFFF;padding-left: 10px">
<p style="font-size:19px; color:#cc2828"> [wpv-post-type show="plural"] </p>
<h5>[wpv-post-link]</h5>
[wpv-post-date]
[/wpv-conditional]

This is for items that are greater than 100

[wpv-conditional if="( '[wpv-loop-index]' gt '100' )"] 

Additional code here

[/wpv-conditional]

Thanks,
Shane

#1197557

Hello Shane,
thanks for the code, one question though, is lte or 100 referring to the 100 last entries, so internally the system is looking at the post ID? Or is it view/loop dependend. What I mean is- does it just "mark" the first 100 entries of the search. (Which would change with every new search - of course.

#1197562

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Michael,

How it works is that the [wpv-loop-index] shortcode is just an index counter that increments each time a new post is displayed.

It actually keeps track of which number post its displaying. The 100 in my previous post is the index at which you want the loop to stop displaying the "New " overlay.

It Marks the first 100 entries and anything beyond the first 100 entries are displayed normally.

Thanks,
Shane

#1200722

Hi Shane,
I tested it - and as I thought it does mark the first 100 entries in the view with my "new" overlay.
But this is not what I need, because it changes based on the filtering or sorting the user does.

What I need is the 100 latest posts - marked with "new" - and this is only possible with the post-id of the displayed post.
Only if the last 100 posts (by ID) are called, they remain the same no matter how the user is sorting or filtering the view.

Thanks
Michael

#1200956

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Michael,

I mean there isn't a way to tell by using the post ID's which are the newest posts. This is done by the post date. So it will complicate things in the filter because lets say your users do a search, however only 2 of your newest 100 posts will show up.

The only way I see this working is by using some custom code to mark a post as new when it is created and then remove the last 100th post from being new.

So some custom code is needed here which would be out of the scope of our support forum. The best I can do right now is to advise of where you can get assistance with the custom code.
https://toolset.com/contractors/

Because the only facility we have for this right now is to use the index counter.

Thanks,
Shane

#1200960

Obviously the post's with the highest ID are the latest posts. So I would need a script that uses the 100 highest post ID#s and then I could mark them as "new"
I thought something like this would be possible with toolset.

But I was wrong.

thanks
Michael

#1200974

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Michael,

It is possible through these means but you have to remember that we need a starting value to get the latest 100. The post ID's are shared amongst all the posts and post types in wordpress.

This means that if CPT A has a post id of 100 and we create a Post in CPT B this will give it an ID of 101 but if we go back to CPT A and create a post it will give us 103.

So at any given point we don't know which value would be the highest. It is also possible that the post ID goes beyond 1000 as well.

It isn't a safe alternative because eventually it would need some re-adjusting to get additional posts that are beyond your starting value.

Another alternative is that you can get all the post ID's and then you sort it and get the ID's of the latest 100 and store it into a shortcode array, then check if the ID of the Post that is being output by view is one that is in that array.

This one seems like it could work.

Please let me know if this helps because we might be able to write up a shortcode for this.

Thanks,
Shane

#1200976

Another alternative is that you can get all the post ID's and then you sort it and get the ID's of the latest 100 and store it into a shortcode array, then check if the ID of the Post that is being output by view is one that is in that array.

This would be fine with me, I am aware that the post ID of all the post includes pages etc. So my 100 would be maybe only 95 if 5 other posts were made. But this is ok, as nobody is counting the latest 100 entries in the list I am marking as "new"

Thanks for your help
Michael

#1201280

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Michael,

So I managed to whip up a custom shortcode for you.

// Add Shortcode
function mark_recent_posts( $atts ) {
	// Attributes
	$atts = shortcode_atts(
		array(
			'post_id' => '',
			'post_type' => '',
		),
		$atts
	);
	$args = array(
		'numberposts' => 100,
		'orderby' => 'post_date',
		'order' => 'DESC',
		'post_type' => $atts['post_type'],
		'post_status' => 'publish',
		'suppress_filters' => true
	);
	$recent_posts = wp_get_recent_posts( $args );
	$arr_id = array();
	foreach($recent_posts as $recent_post){
	    array_push($arr_id,$recent_post['ID']);
	}
  	if(in_array($atts['post_id'],$arr_id)){
      	return 'New';
    }
}
add_shortcode( 'mark_recent_posts', 'mark_recent_posts' );

Add this to the views custom code by going to Toolset -> Settings -> Custom Code and add this as a new snippet and activate it.

So we can use it to mark the the post with a New class. then you can do some custom styling for the "New" class.

Using this snippet in views you will do it like this.


<div class="[mark_recent_posts post_id='[wpv-post-id]' post_type='cpt_slug']">

</div>

So if a post ID occurs in the array of ID's that is generated then it will mark it as "NEW".

Thanks,
Shane

#1201594

My issue is resolved now. Thank you!