Skip Navigation

[Gelöst] Recording number of child posts in a post custom field

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:
Want to record number of child posts in a post custom field.

Solution:
We have a solution for the exact same case here:
https://toolset.com/forums/topic/how-to-order-parent-posts-according-to-their-child-post-count/#post-577316

==> In the code given above, please make sure to replace 'child-post-type-slug' with your original child post type slug.
==> Replace 'parent-post-type-slug' with your original parent post type slug.
==> Replace 'custom-field-slug' with your field slug.

And then in the View >> you will set Ordering >> by your Custom field.

This support ticket is created vor 6 Jahre, 5 Monate. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

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

This topic contains 4 Antworten, has 2 Stimmen.

Last updated by julienL-5 vor 6 Jahre, 5 Monate.

Assisted by: Noman.

Author
Artikel
#578061

Hello -

Here is my setup:
- "book" CPT listing books
- "practitioner" CPT listing practitioners
- "reco-book" CPT listing book recommendations made by practitioners, and which is a child post of "book" and "practitioner" to implement a many-to-many relationship
- CRED form to post new recommendations displayed on the "book" post template: this form just shows the submit button with the "recommend" text, and creates a new "reco-book" post with the correct "book" and "practitioner" parents

- total number of recommenders is displayed in the "book" template correctly by using the following shortcode

// BOOK RECOMMENDERS: shortcode to count them to display count on post page
add_shortcode('book-reco-count', 'book_reco_count_func');
function book_reco_count_func()
{
$child_posts = types_child_posts('book-reco');
return count($child_posts);
};

Now, I want to be able to use the total number of recommenders easily in my Views/Layouts modules. Here, I have 2 questions:

1) It seems the easiest way to do this is to record the total number of recommendations in a custom field. Is this correct or is there a better alternative?

2) If a custom field is the best way, then what is the best way to implement this? I have a working solution using the code below:

// CRED form action to count recommenders
add_action('cred_save_data_160', 'jln_add_book_id',10,2);
function jln_add_book_id($post_id, $form_data)
{
$parent_id = get_post_meta($post_id, '_wpcf_belongs_book_id', true);
$result = new WP_Query(array(
        'post_type'     => 'book-reco', // your child CPT slug
        'post_status'       => 'publish',
        'meta_query' => array(
        array(
            'key'     => '_wpcf_belongs_book_id',
            'value'   => $parent_id,
            'compare' => 'LIKE',
        ),
    ),
        'posts_per_page'    => -1, // can result in performance issues!
    ));
$book_recommenders = count($result->posts);
update_post_meta($parent_id, 'wpcf-total-recommenders-book', $book_recommenders);
}

This works well and basically uses the fact that new Recommendations are always added through the same CRED form to count the total number of recommendations made so far each time the CRED form is submitted and update the appropriate custom post field.

However, this can certainly result in performance issues since I have to count every posts, and this CRED form is going to be heavily used.

Would you be able to point me towards another, simpler and more robust of achieving this? There might even be a built-in way to do this 🙂

Many thanks!

Best,
- Julien

#578109

Noman
Supporter

Languages: Englisch (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Julien,

Thank you for contacting Toolset support.

It seems the easiest way to do this is to record the total number of recommendations in a custom field. Is this correct or is there a better alternative?
>> Total number of recommendations in custom field is not a best approach because when we add new child, we also need to update that field, it takes more processing, shortcode is better approach to display number of recommendations as you are using already.

Is there any other purpose for saving number of recommendations in custom field?

Thank you

#578153

Hi Noman -

Many thanks for your quick reply. My 2 main needs using the total number of recommenders are to:
- display the total number of recommenders on the post template, which works very well with the shortcode

- in View modules, sort the books displayed by the total number of recommendations: this is where I thought using a custom field would work best because it is easy to sort the items displayed by custom field values.

I'm basically open to any method you would suggest that allows me to sort items displayed in a view module based on the total number of recommendations 🙂

Best,
- Julien

#578172

Noman
Supporter

Languages: Englisch (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for providing more details. I see what you are trying to achieve. I think you don’t need to use CRED code for this, instead we have a solution for the exact same case here:
https://toolset.com/forums/topic/how-to-order-parent-posts-according-to-their-child-post-count/#post-577316

==> In the code given above, please make sure to replace 'child-post-type-slug' with your original child post type slug.
==> Replace 'parent-post-type-slug' with your original parent post type slug.
==> Replace 'custom-field-slug' with your field slug.

And then in the View >> you will set Ordering >> by your Custom field.

I hope this helps in resolving the issue. Thank you

#581815

Hi Noman, many thanks for this and my apologies for the long delay in replying, as I've had to take care of another urgent project in the meantime 🙁

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.