Skip Navigation

[Resolved] List taxonomies with new posts

This support ticket is created 6 years, 5 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
- 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+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by morktron 6 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#909654
Screen Shot 2018-06-07 at 2.35.22 pm.png
Screen Shot 2018-06-07 at 2.21.51 pm.png

I'm trying to list taxonomies with posts no older than 45 days, the problem is that it is showing the taxonomy multiple times if there are multiple posts in that taxonomy. So how do I show taxonomies with a new post once only?

Here is the View code:
[wpv-layout-start]
[wpv-items-found]

    <!-- wpv-loop-start -->
    <wpv-loop>

  • [wpv-post-taxonomy type="subject"]
  • </wpv-loop>
    <!-- wpv-loop-end -->

[/wpv-items-found]

and attached is a screenshot of the View query and filter - and the results

Maybe I need some custom conditional code?

#909741

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

The output shown is expected given the View you have created, which loops over *posts* and displays the assigned taxonomies.

You need to come at this from another angle, which is to loop over taxonomy *terms* and display them if they meet your requirements (are assigned to posts published within the past 45 days).

It is not something you can do straight-forwardly with WordPress queries, and so is not something you can do in Toolset without having to involve some custom code.

The steps are

1. create a View which queries the "subject" taxonomy terms
2. in the Loop Output section, output the taxonomy term title (or link), wrapped inside a conditional shortcode which checks a custom function "has_new_posts" for true or false.

See https://toolset.com/documentation/user-guides/conditional-html-output-in-views/ for an introduction to conditional display, and note that you will need to register such a custom function: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

3. Write a custom function to test whether there are any new posts for the current term and return a true or false value accordingly.

Writing such custom code falls outside our support policy. I don't know what your level is. If you are capable of doing that yourself, great, if not, let me know and I will try and help you.

#909814

Thanks for your help Nigel, writing custom PHP functions from scratch is beyond my skillset at the moment - that's why I'm using Toolset.

However there is a developer working on this project too, I'll see if he can work it out and will post the solution here if we are successful.

#910625

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sure, okay. If they get stuck and you need help let me know.

#915558

This is the function the developer wrote to get this working:

/**
 * Find out if there are contemplations for a specific taxonomy
 * within specified number of days
 * 
 * @param  {int}     the number of days
 * @param  {string}  should be 'taxonomy', comes from Toolset
 * @param  {object}  the taxonomy, comes from Toolset
 */
function wpv_conditional_taxonomy_has_newer_posts_than($days, $type, $object) {
	$args = array(
		'post_type' => 'contemplation',
		'tax_query' => array(array(
			'taxonomy' => 'subject',
			'field' => 'term_id',
			'terms' => $object->term_id
		)),
		'date_query' => array(
			'after' => date('Y-m-d', strtotime('-' . $days . ' days')) 
		)
	);
	$query = new WP_Query( $args );

	return $query->post_count > 0;
}

and this is the View:

[wpv-items-found]
	<ul>
	<!-- wpv-loop-start -->
		<wpv-loop>
			[wpv-conditional if="( wpv_conditional_taxonomy_has_newer_posts_than(45) eq true )"]
				<li>[wpv-taxonomy-link]</li>
			[/wpv-conditional]
		</wpv-loop>
	<!-- wpv-loop-end -->
	</ul>
	[/wpv-items-found]