Skip Navigation

[Resuelto] Limit display to only one child post

This support ticket is created hace 6 años, 3 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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)

Este tema contiene 7 respuestas, tiene 2 mensajes.

Última actualización por Ljuba hace 6 años, 3 meses.

Asistido por: Nigel.

Autor
Mensajes
#1085778

==> Tell us what you are trying to do?
I have Payments as child post of several parent posts. One type of the Payments child post is "Online payment". However, client can get more than one Payment Gateways (mean, child posts of that kind), ie PayPal and 2Checkout. I thought to add checkbox "Display as Featured", but I don't know how to limit clients to check it more than once. Creating entire system with listing of such kind of posts just for that purpose will be too much (must be some simple solution).

Is it in View (or Content Template) such kind of 'filter' or conditional statement?

#1086647
1.png

You can see now after I solved previous problem, how I created 3 Gateways (2Checkout, PayPal and PayU). All of them have button and/or link, but I want to add to the head (img 1) only one link (no collapse), sort of favorite Payment Gateway.

P.S. I will wrap it conditionally to leave blank space if client will not have ANY Gateway, but how to limit it to ONLY ONE?

#1087406

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

If you want to avoid running extra queries when it comes to displaying this (i.e. a View with limit=1 and some query filter), then I think the only option is to go ahead and add a checkbox field to mark a payment gateway as preferred.

Which leaves you with the problem that they might mark more than one gateway as preferred.

And the only way to solve that would be a code snippet that you add with the save_post hook that runs a query to get the sibling payment gateway posts and check if any of them have "featured" checked and enforce that only one at a time can.

So you will still need a query, but this is a query when saving the post, which should happen much less often than displaying the post.

#1087455

That was my idea also (checkbox or dynamically loaded select field in CRED), but I couldn't found any example on the support forum or documentation. Is it something like that available?

#1087471

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

You'd have to code that yourself, I haven't come across an example of anyone doing the same.

You can use the save_post hook, which is fired whether a post is updated via the front end or back end, to trigger your code.

The post object is available with that hook, so you can check the post type of what is being saved.

For the relevant post type you get the value of the post meta (get_post_meta) for the "featured" field.

If this post has been set as featured, it means its sibling posts must be unset (can't have more than one featured).

So you would use toolset_get_related_posts to get the sibling posts (there is an example of using that in the abierto shortcode I just updated in the other thread).

Then loop over those sibling posts and set the post meta as required to un-feature those posts.

The following links describe the function required for each of those steps:

https://developer.wordpress.org/reference/hooks/save_post/
https://developer.wordpress.org/reference/functions/get_post_meta/
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
enlace oculto
https://developer.wordpress.org/reference/functions/update_post_meta/

#1087483

OK

I think that is much more productive (less queries) and certainly SIMPLE, to get specific fields for that purpose, that to make all those functions and queries, right?

I mean. the GOAL is how to do it EFFECTIVELY/EFFICIENTLY.

#1089348

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

I don't see any better alternatives.

If users could mistakenly mark more than one payment gateway as featured you either need a View query to get the featured payment gateways and set a limit of 1 (so that only one will be returned, even if there are multiple), or you prevent multiples at the time the post is saved, as described above.

There are a few functions involved, but the point is that you save the payment gateway posts infrequently, but view them often, so more efficient to fix it at the time they are saved.

#1089401

Thanks. All clear.