Skip Navigation

[Resuelto] Are all toolset_get_related_posts required?

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:
The toolset_get_related_posts function of the API has a lot of possible arguments, are they all required?

Solution:
The first 3 are required for one-2-many relationships.

For m2m relationships all 8 arguments are required. (We plan to update this to make it easier to pass only certain arguments and leave others as default.)

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

0% of people find this useful.

This support ticket is created hace 6 años, 5 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)

Etiquetado: 

Este tema contiene 5 respuestas, tiene 2 mensajes.

Última actualización por aaronM-9 hace 6 años, 5 meses.

Asistido por: Nigel.

Autor
Mensajes
#909809

I'm reviewing the documentation at https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/ and it's unclear if all arguments are required. I'm writing a custom shortcode that needs to pull all related posts but I do not require pagination or any limit on the number of records returned, so I prefer not to set a limit or offset. Any clarification on this would be helpful. Thanks.

- Aaron

#910531

Nigel
Supporter

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

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

Hi Aaron

Right now the answer is "it depends".

For one-2-many relationships it is enough to provide the first 3 arguments of toolset_get_related_posts. Default values will be used for the remaining arguments.

For many-to-many relationships you will need to provide the 8th argument, and because of the way you pass the arguments you must therefore pass at least the first 8. (There is also a bug where you cannot pass "-1" for limit to indicate "unlimited", you will need to pass an arbitrarily high number instead in this case.)

We already have an internal ticket about improving this so that the arguments are passed as an array of key-value pairs so that you only need to pass the arguments that you need to specify.

#910664

Thanks, that's helpful, and I think I've figured out what is wrong. So I have the following code and I've confirmed that $a['productid'] is defined properly but my php function chokes when it goes to define $reviews. I don't get an error message - the page simply doesn't load. Removing the 'post_object' line seems to fix the problem, I'm guessing because the items in the array are out of order by not including what would otherwise be between 'parent' and 'post_object'.

It will be nice when they switch to key-value pairs.

  		$reviews = toolset_get_related_posts(
		  $a['productid'],
		  array('product-listing', 'product-review'),
		  'parent',
		  'post_object'
		  );

Going to do a bit more testing just to make sure I'm all set.

- Aaron

#910685

Hi Nigel,

Just testing this out some more and I can't seem to get it to return a list of reviews. Here is my code:

  		$reviews = toolset_get_related_posts(
		  $a['productid'],
		  array('product-listing', 'product-review'),
		  'parent'
		  );

Product-Listing is the parent post and Product-Review is the child post (many reviews to one product). I have entered and published a review and verified it is connected to a product, yet it's not returning any results. Just thought I'd check in and make sure I'm setting the attributes properly for the toolset_get_related_posts() function.

- Aaron

#911027

Nigel
Supporter

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

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

Hi Aaron

Is this a legacy relationship? That's the only time you should use an array as the second argument, it should normally be the relationship slug.

I described the arguments in a little more detail for another user here: https://toolset.com/forums/topic/conditional-display-based-on-number-of-parents/#post-906135

Does that help?

#911058

Hi Nigel,

That did the trick. It is a migrated relationship but the specific post connection was created after the migration. I updated the code as per below and now it works!

  		$reviews = toolset_get_related_posts(
		  $a['productid'],
		  'product-listing_product-review',
		  'parent'
		  );

Thanks!

- Aaron