Skip Navigation

[Resolved] Adding up the values of RFG

This support ticket is created 4 years, 4 months ago. 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.

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+01:00)

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by deepS 4 years, 4 months ago.

Assisted by: Nigel.

Author
Posts
#1397257

Hi,
I have a job site where I use no. of vacancies (RFG) as custom fields. Can you help me to create a shortcode to sum up the values of the field mentioned?
Thank you.

#1397463

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

What's the specific structure, here?

What is the parent post type which the RFG belongs to?

What is the field within the RFG that contains the number you want to create a total for?

Where will the shortcode be inserted? Into a template for the parent post, and it will add up the values from the specific field in all of the child RFGs?

#1397577
Screenshot_1.jpg

Thanks for your reply.
Let me explain things elaborately.
I am using job posts as custom posts where I use No. of Vacancies (RFG) against each vacant position. One job post has single custom fields as well as RFGs (to depict each job position). You can have a look at this URL to understand it hidden link
See the Screenshot_1

-What is the parent post type which the RFG belongs to?
jobs

-What is the field within the RFG that contains the number you want to create a total for?
no-of-vacancies

-Where will the shortcode be inserted? Into a template for the parent post, and it will add up the values from the specific field in all of the child RFGs?
I want to show it in Jobs Archive against the specific post.

Hope I can make you understand. If anything else required please let me know. Thanks

#1397783

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

I forgot to ask what the slug of the RFG was, but I've prepared an example shortcode where you can edit the parent CPT slug, the RFG slug, and the custom field slug at the top.

This really should be work that's done by a developer rather than support, but here's an example you can edit and use for your needs. It registers a custom shortcode "sum-rfg".

add_shortcode('sum-rfg', function () {

    $parent_cpt_slug = 'job';
    $rfg_slug = 'vacancies';
    $field_slug = 'number-of-vacancies';

    global $post;
    
    // get RFG posts belonging to current parent
    $rfgs = toolset_get_related_posts( $post, $rfg_slug, array( 
        'query_by_role'     =>  'parent',
        'role_to_return'    =>  'child',
        'return'            =>  'post_id',
        'limit'             =>  -1
        )
    );

    $running_total = 0;

    foreach ( $rfgs as $rfg ){
        $field_value = get_post_meta( $rfg, 'wpcf-'.$field_slug, true );
        error_log('field_value: ' . print_r($field_value, true));
        $running_total += $field_value; 
    }

    return (string) $running_total;
});
#1398611
Screenshot_2.jpg
Screenshot_1.jpg

Hi,
I have tried the code suggested by you but seems to be not working properly. You can see the front-end view in screenshot_1.
I have added the shortcode and changed the RFG slug as well as the field name slug (see the screenshot_2)

If you wish I can share the login credentials for further check. Thanks.

#1398863

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

The fact that the shortcode is being printed as [sum-rfg] rather than being replaced with... something... means that the code to register the shortcode is not being triggered.

So I would double-check that the code snippet you added is activated.

#1399347

My issue is resolved now. Thank you!

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