Skip Navigation

[Resuelto] View to display child post count not working when logged out

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

Problem:
Show the total count of related post

Solution:
Toolset offers post relationship API function toolset_get_related_posts to fetch the related posts.

You can find the proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/view-to-display-child-post-count-not-working-when-logged-out/#post-1098622

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

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

Etiquetado: ,

Este tema contiene 12 respuestas, tiene 2 mensajes.

Última actualización por prasadS hace 6 años, 2 meses.

Asistido por: Minesh.

Autor
Mensajes
#1093269
logged-in.png
logged-out.png

I am trying to: Display the total child post count on the parent page

The total count is correctly displayed when I am logged in into the website. However once I log out, the count shows as zero. (Please see the attached screenshots - logged-in.png and logged-out.png

Link to a page where the issue can be seen: enlace oculto

This is happening for all the posts in this custom post type. Not only for the link above.

I expected to see: Please see the screenshot attached.

Instead, I got: Please see the screenshot attached.

#1093875

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - could you please tell me how you are displaying the total counts using which shortcode so I can test with my local test install and try to reproduce the issue.

Additionally - the post type you are displaying is in post relationship (parent/child)?

#1093919

Well - could you please tell me how you are displaying the total counts using which shortcode so I can test with my local test install and try to reproduce the issue.

1) I am displaying the count using a view. The shortcode for the view is [wpv-view name="total-order-count"]

The code for the view is placed in Loop Editor:

[wpv-layout-start]
	[wpv-items-found]
    [wpv-found-count]
	<!-- wpv-loop-start -->
	<wpv-loop>
		[wpv-post-body view_template="Loop item in Total Order Count"]
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		[wpml-string context="wpv-views"]0[/wpml-string]
	[/wpv-no-items-found]
[wpv-layout-end]

Loop item in Total Order Count is empty.

Additionally - the post type you are displaying is in post relationship (parent/child)?

2) The post type with the slug 'activity' has a one to many relationship with the post type with the slug 'order-count'.
This was earlier a parent- child relationship which was migrated to the new relationship.

#1094769

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

well - this is really strange. I just added the shortcode [wpv-found-count] just before the view's look as you added and check with or without login but I do not able to reproduce the issue.

I can see the total number of counts displays with or without login. I need to check on your install whats going wrong there.

*** 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 would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

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

#1094914

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Well - I see you are using number of third party plugin and you also acknowledge that you find the culprit that causing the issue.

As I also mentioned I do not able to reproduce the issue with minimum setup but Yes, I can confirm that I can see the issue on your site here: enlace oculto

Maybe there is conflict between the 'Permalinks Customizer' or any other third party plugin you are using. If you confirm that issue is with 'Permalinks Customizer' plugin - I will try to reproduce it and if I confirm the issue, I will report it to our compatibility department.

To do so, I need plugin zip or link from where I can download the plugin.

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

#1094941

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Well - I just checked by installing the plugin and adding the custom permalink for one of the custom post type and I still not do not have any issue with total post count.

This is something really specific to your page and site where its not working. So I suggest to isolate the issue and report to us if the issue belongs to Toolset. However, I do not see here that Toolset is issue here.

#1095123

1) I deactivated all cache plugins / disabled cache through cPanel / web hosting.

I deactivated all plugins one by one (except all toolset plugins) and then switched to the default WordPress theme. The issue persisted till the time I deactivated the Permalinks Customizer plugin.

I am confused because the total reviews count - which is displayed using the view [wpv-view name="activity-reviews-total-count"] is showing the count properly.

But, the total orders count, which is displayed using the view [wpv-view name="total-order-count"] is not showing the count when logged out.

I compared both the views and they are exactly the same. Both the views display the child posts count (one to many) on the parent page.

2) I have another view named [wpv-view name="total-order-count-on-view"] which displays the order count inside another view.

The order count is showing up properly on the homepage enlace oculto
(The order count on the view used on the homepage is [wpv-view name="total-order-count-on-view"]

Whereas the order count us not showing up on the page enlace oculto which uses the exact same view [wpv-view name="total-order-count-on-view"].

Can you please guide me with the next steps I should perform / how I can find the issue?

#1095897

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

This is really strange.

I deactivated all plugins one by one (except all toolset plugins) and then switched to the default WordPress theme. The issue persisted till the time I deactivated the Permalinks Customizer plugin.
==> You mean once you deactivate Permalinks Customizer plugin - issue is solved? If yes:

There must be a unknown conflict or something thats causing this. To debug this issue deeply I need duplicator copy of your site to debug this issue further:
=> https://toolset.com/faq/provide-supporters-copy-site/

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

#1098622

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Well - to display the only count using view is not a wise idea I would say.

Toolset offers post relationship API function toolset_get_related_posts to fetch the related posts.
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

So, I've created a new snippet with the custom shortcode which will fetch the total count.
=>enlace oculto

function func_get_total_count( $atts ){
	global $post;
 
 $childs = toolset_get_related_posts(
  $post->ID,     // the parent post
  array('activity','order-count'),   // the relationship slug
  'parent',     // the    $post->ID role in this relationship 
  10000,     // the maximum number of results
  0,           // the offset
  array(),     // additional query arguments
  'post_id',   // return format
  'child',    // role to return
  '',
  'ASC'
  );
  return count($childs);
}
add_shortcode( 'show_total_count', 'func_get_total_count' );

And I just called it just besides your view in your layouts cell:
=> enlace oculto

 [wpv-view name="total-order-count"] ===== [show_total_count]

I see the shortcode [show_total_count] is working fine and displaying the correct count with or without login. Could you please confirm.

#1098722

I checked this on the staging website and on the main website as well. Works perfectly fine.

I used the short code on a layout and also inside a view. Works when logged in and logged out. Below are the links.
enlace oculto
enlace oculto

For all other one to many post types, can I use a similar php code, to get the total count? For example, 'activity' and 'reviews' ?

Thanks again!

#1099508

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Yes - exactly. That is the correct way as it will fire only one query rather looping through the view's loop.

If you face any problems, please create a new ticket and feel free to assign it to me. It will be pleasure to help you out.

#1100352

Thank you very much for the help 🙂

I will now replace the views I have created to display the count, with the new short codes.

#1510569

Hi Minesh

I have created a snippet from your code to display the total reviews count. The relationship between Reviews and Campsites is one-to-many. Each campsite can have multiple reviews. The code you had shared worked fine for 'activities' and 'order'counts. Can you help with this?

I want the shortcode to display the total review count for that each post (campsite).

function func_get_total_campsite_review( $atts ){
	global $post;
 
 $childs = toolset_get_related_posts(
  $post->ID,     // the parent post
  array('campsite','review'),   // the RFG slug
  'parent',     // the RFG role in this relationship is 'child'
  10000,     // the maximum number of results
  0,           // the offset
  array(),     // additional query arguments
  'post_id',   // return format
  'child',    // role to return
  '',
  'ASC'
  );
  return count($childs);
}
add_shortcode( 'show_total_campsite_reviews', 'func_get_total_campsite_review' );

Thanks in advance.