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?
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
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
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".
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
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>
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.
Great, that is perfect thanks!
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
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.