Skip Navigation

[Résolu] Display an icon when a new post is published.

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
How to display a badge or notification that there is new content on the site published or modified within the past, say, 48 hours?

Solution:
You would create a View that queries the kinds of content you want to check the publish/modify date for and outputs the badge or message you want to display, e.g. "Fresh content!". You can probably disable the wrapper div for a simple View such as this.

Then you need to add some code that uses the Views API hook wpv_filter_query to modify the query so that only posts modified within the target period are returned. An example of such code is given below: https://toolset.com/forums/topic/display-an-icon-when-a-new-post-is-published/#post-1224497

This support ticket is created Il y a 5 années et 7 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

Ce sujet contient 15 réponses, a 2 voix.

Dernière mise à jour par larryL Il y a 5 années et 6 mois.

Assisté par: Nigel.

Auteur
Publications
#1218440

is there a query/filter I can create that I can use to display a png when a new post is created or an existing post is modified is the last X hours? Thanks!

#1218820

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+01:00)

Hi Larry

You need to use conditional statements for this.

You can read more about them here: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

Note the option SECONDS_FROM_NOW is described here: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

https://toolset.com/documentation/user-guides/date-filters/

Here is an example from the output of a View where I display the title of a post and, using a conditional statement, add a "recent!" flag if the post was modified within the last hour (3600 seconds).

		<wpv-loop>
          <h3>[wpv-post-link] [wpv-conditional if="( '[wpv-post-date type='modified' format='U']' gt 'SECONDS_FROM_NOW(-3600)' )"]recent![/wpv-conditional]</h3>
		</wpv-loop>
#1221175

All this does is display a list of recent posts.

All I want to do is display a PNG when there is either a new post or an update to an existing post.

#1221669

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+01:00)

Where are you supposed to be displaying this?

I had taken your question to be when displaying posts add a flag to those which are new or have been updated recently.

You might also want to clarify what new or recent mean.

#1222074

Well, I was gonna try to insert this on the tab for the list of articles. But the 'where' isn't really important at the moment. It's the how can I get this 'image' view to work. Since I've already got the views with the list of posts.

#1222189

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+01:00)

Sorry, it might not seem important to you, but I can't help you with it if I don't fully understand what you are trying to do.

So, you already have a list of posts created with a View.

This new flag appears one time, not for each post, and it appears somewhere other than the View itself?

It should just be a badge that you can insert anywhere, if there are any posts that have been updated/created in the last x hours, something like that?

#1224150

Sorry Nigel, I didn't mean it to come across as flipitantly as that. I just meant that I was looking for a specific result to appear and that it could show up in a test page for the moment as a proof of concept. Ultimately it will be inserted into a Tab Title (I think).

So all I really want to do is display a 'new post image png' when there is a new post (based upon specific filters). So if a new post is added, or a post is modified, would work fine. If we could get the image to 'expire' after say 48hrs that would be great.

Does this help?

#1224497

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+01:00)

Hi Larry

OK, so I set up a View on my local test site.

It queries whatever post types should be considered as representing new content on the site.

It includes a Limit of 1 (i.e. don't need more than one result), and in the output section I'm outputting text that says "New posts!", but you could output an image tag with your "New!" png, or whatever. In my case I disabled the wrapper div, it's likely not needed.

Now we are just missing the date filter, to include posts published within the last 48 hours, for example.

The WordPress date query class includes some nice settings ideal for this, but unfortunately they are not available in the Views GUI, where it is actually rather hard to do without some custom code.

So, here is a snippet I used to add a filter to include posts updated (which includes created) within the last 48 hours.

You would need to edit the View ID, and the date condition as required, but otherwise it should be pretty self-explanatory.

function tssupp_filter_query($view_args, $view_settings, $view_id)
{
    if (in_array($view_id, array(159))) {

        $view_args['date_query'] = array(
            'after'     =>  '48 hours ago',
            'column'    =>  'post_modified'
        );
    }
    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_query', 101, 3);

Would something like that work for you?

#1224588

Hi Nigel,

This would work great but I'm running into a problem.

I added the function and modified the view ID to the new view that I created to display the placeholder text in the Output Editor.

I selected the following:

Posts
Don't include currrent (on)
Order by Post Date
Display 1 Items...
Taxonomy Filter 1st - none. 2nd time - All of the followin (documents) 3rd time - any of the following (documents)...
No Pagination
Full page refresh (by default)
No Search/Pagination code
Loop Editor has just: [wpv-post-body view_template="loop-item-in-dox-new-post-notification"]
Loop item in DOX field has 'test image' text in it
Diable wrap div checked
Output editor- 'New Posts!' text

Nothing shows up on my text page assigned to the view. Yes, I did create a new 'test' post. What am I forgetting/missing?

Larry

#1224937

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+01:00)

Sounds good.

Trivial question, but where did you add the custom code? If you added it as a snippet at Toolset > Settings > Custom Code did you remember to activate it?

Otherwise, can I get access to your site and I can take a look to see if I can spot the problem?

I will mark your next reply as private so that I can get log-in credentials from you—you may want to create a temporary admin user for me to use that you can later delete. And be sure to have a current backup of your site.

What is the View and where did you insert it?

#1225909

I didn't add any custom code. I added a function as you requested. Was there something else you wanted me to do?

#1225914

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+01:00)

The function (and filter) shared in https://toolset.com/forums/topic/display-an-icon-when-a-new-post-is-published/#post-1224497 is the custom code.

Where did you add it?

#1225957

sorry, functions.php

#1226306

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+01:00)

It's fairly simply, so I'm not sure why it wouldn't work.

Can I get access to your site to look? Next reply is private.

#1227098

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+01:00)

Screenshot 2019-04-08 at 10.40.39.png

Hi Larry

I checked your View and it seems you had edited the Output Editor section and removed the required shortcode that outputs the results of the View.

See my screenshot where I expanded the formatting instructions.

I added the required wpv-layout-meta-html shortcode and checked the page on the front-end where this View is inserted and it works. (You don't need the filter shortcode in this case because you don't have any filters in this View.)

Ce ticket est maintenant fermé. Si vous êtes un client de Toolset et que vous avez besoin d'aide, veuillez ouvrir un nouveau ticket d'assistance