Skip Navigation

[Résolu] Conditional display based on number of parents

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

Problem:
The client needs a custom function that returns the number of parent posts attached to the current post.

Solution:
The new relationships API includes the function toolset_get_related_posts can be used for this, the documentation is linked to below.

Take careful note of the parameters, because the first *8* are required, as described here: https://toolset.com/forums/topic/conditional-display-based-on-number-of-parents/#post-906135

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

This support ticket is created Il y a 6 années et 6 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+00:00)

Ce sujet contient 8 réponses, a 3 voix.

Dernière mise à jour par toolset-dave Il y a 6 années et 6 mois.

Assisté par: Nigel.

Auteur
Publications
#905425

Hi,

I am rebuilding my site about ebooks using many-to-many relationship.

On page for "ebook" I would like to have a conditional display based on number of parents "author".

If number of parents is 1 then show word "Author", if 2 or more then show "Authors".

#905551

You would need to use the new Relationships API to craft a ShortCode that counts the number of posts.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

This function will allow you to get all connected posts in a relationship.
Then, you can, for example, use the native PHP Function "count()" to count how many post ID's you received and return that in a ShortCode.
lien caché

This ShortCode then would need to be registered in the Toolset > Settings for safety purpose-
https://toolset.com/documentation/user-guides/shortcodes-within-shortcodes/

#905591

Nigel
Supporter

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

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

Hi there

I think your best option here is to write a PHP function that uses the new relationships API function toolset_get_related_posts to return the number of related posts.

You can then register that function to be used in conditional shortcodes at Toolset > Settings > Front-end Content.

The API is documented here: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Using functions in conditional shortcodes is described here: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

If you try and get stuck, let me know.

#905676

Hi,

thank you for the links.

I have created this shortcode, but I am stuck with it. It returns me only 0.

add_shortcode('autor-count', 'autor_count_func');
function autor_count_func()
{
$autori = toolset_get_related_posts(
    get_the_ID(),
    array( 'autor', 'ebook' ),
    'parent',
    100,
    0,
    array(),
    'post_id');
return count($autori);
} 
#906135

Nigel
Supporter

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

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

Is this a new M2M relationship rather than a migrated relationship?

The arguments for toolset_get_related_posts are:

[php]
1. post ID or post object from one end of the relationship
2. relationship slug (for new relationships) OR an array of the connected post types for old relationships,
3. role of 1 above in the relationship (e.g. we are starting from the parent)
4. limit
5. offset
6. args array (for meta queries)
7. return post_id's or post_object's
8. role of posts to be returned (e.g. if 3 is parent this would be child)

The remaining arguments are optional, but you need to supply all of the above.

Note that it is possible to return the post count in a variable you specify if you also supply the remaining arguments, so that you wouldn't have to use count().

#906348

Hi,

I have modified the code as you recommended and it works:

add_shortcode('autor-count', 'autor_count_func');
function autor_count_func()
{
$autori = toolset_get_related_posts(
    get_the_ID(),
    'autor_ebook',
    'child',
    100,
    0,
    array(),
    'post_id',
    'parent');
return count($autori);
}

I also tried to specify the remaining arguments as you wrote, but this doesn´t work:

add_shortcode('autor-count', 'autor_count_func');
function autor_count_func()
{
$autori = toolset_get_related_posts(
    get_the_ID(),
    'autor_ebook',
    'child',
    100,
    0,
    array(),
    'post_id',
    'parent',
    null,
    'ASC',
    true,
    100);
return $autori;
}  

And I am not sure what is meant by the offset. Can you explain it to me? Thank you.

#906668

Nigel
Supporter

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

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

The offset is typically used for pagination.

Together with limit you might say "show me 10 results starting from the 11th result" which would effectively be showing the second page of results where you have 10 results per page.

Regarding your second code sample, the final parameter should be a variable name, which will then contain the number of results.

So if you change the last argument to $found_rows, for example, then you should be able to return $found_rows rather than $autori.

I haven't tested it myself, you are passing the variable by reference, it may be that you have to declare the variable before using it as an argument in toolset_get_related_posts (e.g. set it to zero first).

#906731

Thank you, Nigel, for your explanation and help. I modified the second code and its working fine. 😉

#906732

Resolved