Skip Navigation

[Résolu] Shortcode to count number of posts a user has made of a certain type

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

Problem: I would like to show the number of posts a particular user has made of a specific post type. When I use Views, extra HTML is added that breaks my site's formatting.

Solution: Use a custom shortcode to count the number of results in a View using get_view_query_results() and count() in PHP:

// return the number of results in a View with no formatting
add_shortcode( 'ts_view_num_results', 'ts_view_num_results_func');
function ts_view_num_results_func($atts)
{
  $view_id = intval($atts['viewid']);
  $results = get_view_query_results( $view_id );
  return count($results);
}

Relevant Documentation: https://codex.wordpress.org/Shortcode_API
https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results
http://php.net/manual/en/function.count.php

This support ticket is created Il y a 6 années et 8 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.

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Marqué : 

This topic contains 8 réponses, has 3 voix.

Last updated by Christian Cox Il y a 6 années et 6 mois.

Assisted by: Christian Cox.

Auteur
Publications
#555169
Screen Shot 2017-08-02 at 12.19.03 AM.png
Screen Shot 2017-08-02 at 12.18.55 AM.png

Code should be reasonably self-explanatory. Just trying to make a shortcode to count the number of Enquiries a user has submitted:

// Customer Enquiry Count
add_shortcode('customer-enquiry-count', 'PMR_customer_enquiry_count');
function PMR_customer_enquiry_count($user_id, $post_type = array('enquiry')) {
$args = array(
'post_type' => $post_type,
'author' => $user_id,
'post_staus'=> 'publish',
'posts_per_page' => -1
);

$query = new WP_Query($args);

return $query->found_posts;
}

I've got test users set up who definitely have posts of the type "enquiry" (see attached screenshots)

However, it always returns "0". Any ideas why this might be?

#555246

Hi,
I'm not able to see how the shortcode is implemented, so I can't say for sure, but you may not have the $user_id variable set correctly. Also I noticed there is a typo in the array key "post_status" here:

'post_staus'=> 'publish',

Out of curiosity, is there a specific reason for using a custom shortcode? You can accomplish this in Views without any custom code:
- Create a View of the 'enquiry' post type, filtered by author, and pass in the author ID as a URL param or shortcode param, or using the current user ID.
- In the Loop Output editor of this View, you can place the [wpv-found-count] shortcode just before the <wpv-loop> tag, and remove any content inside the <wpv-loop> tag so nothing else is output besides the post count. More info about this shortcode here:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-found-count
- Instead of your custom shortcode, place the View

#555608
Untitled.png

Hi Christian,

Hmm, to be honest I hadn't thought of doing it like that (first toolset project and all.)

So i've got the counters working now, but toolset adds a heap of extra formatting in there that I don't need (see screenshot). All I want is the number "2" beside enquiries, to denote that the customer has 2 enquiries open.

Is there a way to just output the raw "2" without all the superfluous formatting?

Cheers,
Brad

#555679

First, clean up your View. Remove any extra code and delete everything between the <wpv-loop> tags in your View's Loop Output editor. Your code should look like this:

[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]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

If this does not resolve the problem, I need to see how this View is added to the content of this page. If it's a shortcode added to some content, I need to see that code. I also need to see the complete HTML generated for the My Enquiries button, including the "2".

#555850
Screen Shot 2017-08-03 at 8.42.50 AM.png
Screen Shot 2017-08-03 at 8.42.19 AM.png

Thanks for getting back to me. See attached screenshots for how it's all set up.

All I need is for the view to return

2

instead of

<div id="wpv-view-layout-541-TCPID49" class="js-wpv-view-layout js-wpv-layout-responsive js-wpv-view-layout-541-TCPID49" data-viewnumber="541-TCPID49" data-pagination="{"id":"541","base_permalink":"/customer-portal/?wpv_view_count=541-TCPID49&wpv_paged=WPV_PAGE_NUM","query":"normal","type":"disabled","effect":"fade","duration":500,"speed":5,"pause_on_hover":"disabled","stop_rollover":"false","cache_pages":"enabled","preload_images":"enabled","preload_pages":"enabled","preload_reach":1,"spinner":"builtin","spinner_image":"<em><u>hidden link</u></em>","callback_next":"","manage_history":"disabled","has_controls_in_form":"disabled","infinite_tolerance":"0","max_pages":0,"page":1,"loop":{"type":"","name":"","data":[],"id":0}}" data-permalink="/customer-portal/?wpv_view_count=541-TCPID49"><a class="x-btn x-btn-square x-btn-mini x-btn-block" href="/customer-portal/my-enquiries/" data-options="thumbnail: ''" style="outline: none;">2</a></div>
#556122

Okay I see, there's no good way to strip out this extra markup. You can restyle it with CSS, or implement something like this custom shortcode:

// return the number of results in a View with no formatting
add_shortcode( 'ts_view_num_results', 'ts_view_num_results_func');
function ts_view_num_results_func($atts)
{
  $view_id = intval($atts['viewid']);
  $results = get_view_query_results( $view_id );
  return count($results);
}

Use it like this:

[ts_view_num_results viewid="4"]

Replace the "4" with the numeric ID of any View, and this shortcode will return the number of results found.

#556455

Great, that is perfect thanks!

#578930

Hi,

My goal is to hide html in content template if view has 0 results.
I displaying view in my content template but with some taxonomyfilter.
I have used above code but it is giving me wrong result count.

[ts_view_num_results viewid="960"]
[wpv-view name="all-events-listing" limit="2" wpvcompetencecentercategory="[types field='event-category'][/types]"]

Thanks

#579083

Marc, this shortcode is not designed to work with filters or limits applied to the View using shortcode attributes. It's only designed to work with Views that have their limits and filters applied through the Query Filter controls.

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