Skip Navigation

[Resolved] Child posts count

This thread is resolved. Here is a description of the problem and solution.

Problem:
Child posts count

Solution:
User were using the the wrong funtion to fetch the child posts. You need to usee the Types post-relationship API function toolset_get_related_posts() and wrap with the shortcode.

https://toolset.com/forums/topic/child-posts-count/#post-1228347

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

This support ticket is created 5 years 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 9 replies, has 2 voices.

Last updated by Sasank 5 years ago.

Assisted by: Minesh.

Author
Posts
#1226963

There is a companies listing page, each company may have few jobs (post relation). I want to show an indication if the company has jobs.

I tried using shortcode mentioned here https://toolset.com/forums/topic/show-at-total-post-count-for-custom-post-type/ to get number of jobs in a company and set condition. But, I am not getting write number,

add_shortcode('postcount', 'post_count');
function post_count($atts) {    
  	$type = $atts['type'];  
    $count_posts = wp_count_posts($type);
    $published_posts = $count_posts->publish;
    return $published_posts;
} 

[wpv-conditional if="( '[jobs-count]' ne '0' ) "]<div class="hiring">Hiring</div>[/wpv-conditional]

I get right count when I loop in view, but I am not able to use the view code for wpv-conditional

[wpv-layout-start]
	[wpv-items-found][wpv-found-count]<!-- wpv-loop-start --><wpv-loop></wpv-loop><!-- wpv-loop-end -->[/wpv-items-found]
	[wpv-no-items-found]0[/wpv-no-items-found]
[wpv-layout-end]

For testing I added [jobs-count] shortcode as condition and showing number of jobs using view on this page hidden link

[wpv-conditional if="( '[jobs-count]' ne '0' ) "]<div class="hiring">[wpv-view name="company-jobs-count"] Job(s)</div>[/wpv-conditional]

Can you please suggest me a solution for it?

#1226970

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - I need to review your jobs-count shortcode as well as can you please share problem URL where you added the conditional statement with jobs-count shortcode.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1227018

Hi Manesh,

I already mentioned the URL where the problem is hidden link

You can see 2 numbers before and after "Job(s)" (on top of logo on left) the 1st number is coming from view and 2nd number is coming from shortcode. You can see the code below I am using,

[wpv-conditional if="( '[jobs-count]' ne '0' ) "]<div class="hiring">[wpv-view name="company-jobs-count"] Job(s) [jobs-count]</div>[/wpv-conditional]

Also if you see in the link hidden link few companies is working from when there are no jobs but in few even if there are no jobs still getting some random number from shortcode but from view getting the count correctly.

I will not be able to provide access to the site as it is a production site and dont have a test setup for the same!

Thanks,
Sasank

#1227089

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

When you use custom shortode as Types shortcode's attribute argument, you need to register the custom shortcode at:
=> Toolset => Settings => Frontend content Tab => Third-party shortcode arguments

In addition to that, you need to set your view to use list with separators and "Disable the wrapping DIV around the View" checkbox on your edit view page.
https://toolset.com/documentation/user-guides/view-layouts-101/#list-with-separators

More info:
=> https://toolset.com/documentation/user-guides/shortcodes-within-shortcodes/#third-party-shortcode-arguments

#1227589
Capture.JPG

I had already registered the shortcode in settings.

Sorry but I didnt understand how would list with separators would help because If I select that option I will be able to add only one field and to show company list I will be needing multiple fields (currently using Content Templates for the view) And if I disable warping div the ajax filters wont work right? If I dont use ajax filters I am being redirected to Taxonomies archive page because the filters are based on custom taxonomies.

#1227620

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - I got you now.

Well - for me it's impossible to help you or guide you in the right direction until I review your shortcode and to find whats causing the issue.

Its Ok if you will share just wp-admin access details and tell me where exactly you added the jobs-count shortcode? to your themes functions.php file?

As we just need to deal with jobs-count shortcode it will not affect on other pages on your site.

I have set the next reply to private which means only you and I have access to it.

#1227775

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - thanks for sharing access details.

But when I try to login using shared wp-admin access details it asks me to add further authentication code:

An Authentication Code has been sent to the email address associated with your account. Look for an email with "Login Authentication Code" in the subject line.

Can you please remove this authentication for now so I can successfully log in to your site and check the issue.

#1228248

Sorry about that! you should be able to login now

#1228347

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - the function you ware using to fetch the child posts was wrong. You need to usee the Types post-relationship API function toolset_get_related_posts().

I've adjusted the shortcode as given under:

add_shortcode('jobs-count', 'jobs_count_func');
function jobs_count_func($atts) {
  global $post;
  
  $child_posts = toolset_get_related_posts($post->ID,'company-job','parent',999,0,array(),'post_id','child');
  return count($child_posts);  
}

I can see now its displaying correct results now:
=> hidden link

More info:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

#1228349

I had tried toolset_get_related_posts() before reading in docs but didnt work may be there was some syntax error. It seems to be working now.

Thank you Minesh

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