Skip Navigation

[Résolu] Use Views output as arguments in wpv-conditional shortcode?

This support ticket is created Il y a 6 années et 2 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Marqué : 

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

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

Assisté par: Waqar.

Auteur
Publications
#1117680

I have two Views. The first selects all posts of type 'A' and adds together the values found in custom field wpcf-total-days:-

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->

	[calculate round=2]
		<wpv-loop>
          [wpv-item index=1][types field="total-days"][/types][wpv-item index=other]+[types field="total-days"][/types]
		</wpv-loop>
	[/calculate]

	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		0
	[/wpv-no-items-found]
[wpv-layout-end]

The second View returns the number of type 'B' posts that exist:-

code>[wpv-layout-start]
[wpv-items-found]
[wpv-found-count]
<!-- wpv-loop-start -->
<wpv-loop>
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
0
[/wpv-no-items-found]
[wpv-layout-end][/php]

Type 'B' posts are related to type 'A' posts in a one to many relationship (A being the parent, B being the child).

I'm looking, if possible to display content if the total number of days across all type A posts is greater than the number of type 'B' posts found. I tried this but the page just hangs:-

[wpv-conditional if="( '[wpv-view name="post-count_total-days"]' gt '[wpv-view name="found-count_type-b"]'  )"]Some content[/wpv-conditional]

Am I trying to achieve the impossible or is there a better/right way of achieving this please?

#1118287

Hi Julie,

Thank you for contacting us and I'll be happy to assist.

For a better performance and consistency, custom shortcodes can be used when a complex comparison is involved.

For example, you can add the following code, at the bottom of your active child theme’s “functions.php” file:


// custom shortcode to check if user image is set
add_shortcode('check_count_condition', 'check_A_B_condition_func');
function check_A_B_condition_func() {

	$slug_post_type_a = "post_a";
	$slug_post_type_b = "post_b";
	$custom_field_days_a = "wpcf-total-days";

    
	// get total number of days from all post types A
   
   $args_a = array(
	'posts_per_page'   => -1,
	'post_type'        => $slug_post_type_a,
	'post_status'      => 'publish',
   );

   $posts_a_array = get_posts( $args_a );

   $posts_a_days_count = 0;

   foreach ( $posts_a_array as $post_a ) {
     $posts_a_days_count = $posts_a_days_count + get_post_meta( $post_a -> ID, $custom_field_days_a, true );
   }

	// get total number of post types B

   $args_b = array(
	'posts_per_page'   => -1,
	'post_type'        => $slug_post_type_b,
	'post_status'      => 'publish',
   );

   $posts_b_array = get_posts( $args_b );

   $posts_b_count = count($posts_b_array);

   // return "1" if total number of days from all post types A is greater than total number of post type B or else "0"
   if ($posts_a_days_count > $posts_b_count)
   {
   	return 1;
   }
   else
   {
   	return 0;
   }
}
 

Note: please remember to update the values of “$slug_post_type_a”, “$slug_post_type_b” and “$custom_field_days_a”, as they’re being used on your website.

Next, register this new shortcode [check_count_condition] with Toolset, so it can be used inside its built-in shortcodes, as explained in the following guide:
https://toolset.com/documentation/user-guides/shortcodes-within-shortcodes/

After that, you’ll be able to wrap your conditional content, using the following conditions and without the extra views:


[wpv-conditional if="( [check_count_condition] eq '1' )"]
Some content when condition is true
[/wpv-conditional]


[wpv-conditional if="( [check_count_condition] eq '0' )"]
Some content when condition is false
[/wpv-conditional]

I hope this helps! Please let us know if you need any further assistance.

regards,
Waqar

#1119782

Hi Waqar

That's absolutely fantastic - thank you for your help!