Hi there,
I have a LearnDash installation and am trying to create a slider of topics.
I'm adding a view and specifying that the views shortcode should have an attribute named "ids" and am passing topic post IDs to that attribute so the view filters by those post IDs.
I'm running the shortcode in a PHP function like so:
$shortcode_content = sprintf ( '[wpv-view name="%1$s" ids="%2$s"]',
'caf-view-of-topics',
implode(',',$topic_ids ) ) ;
$shortcode_return_value = do_shortcode( $shortcode_content ) ;
I can see that post IDs are being populated in the $topic_ids array. The issue is that the return value of the shortcode is "No items found," indicating no posts were found. The view name is correct. I think the syntax for the contents of do_shortcode() is correct. Can someone see what I'm doing wrong?
Thank you.
Saul
Hi Saul,
Thank you for contacting us and I'd be happy to assist.
I've tested your code and it works as expected on my test website.
Here is how I tested its output in a custom shortcode:
add_shortcode('show_view_output', 'show_view_output_func');
function show_view_output_func() {
$topic_ids = array(68,69,75);
$shortcode_content = sprintf ( '[wpv-view name="%1$s" ids="%2$s"]', 'view-to-show-books-ids-filter', implode(',',$topic_ids ) ) ;
$shortcode_return_value = do_shortcode( $shortcode_content );
return $shortcode_return_value;
}
I'll recommend debugging exactly which post IDs are being received through $topic_ids and testing the same IDs in the view shortcode directly to see if the view brings in any results.
Also, make sure that the page is not showing any cached output.
regards,
Waqar
Waqar,
Thanks for your response. I've modified my code to hardcode the value of a single post ID, and still the "No items returned" message persists. Can I give you access to a copy of my site to investigate futher?
Saul
Thanks for writing back.
You're welcome to share temporary admin login details, along with the link to the page where you're using this. Also include the information about where and how this code is being added.
Note: Your next reply will be private and it is recommended to make a complete backup copy, before sharing the access details.
Thank you for sharing the admin access.
I tested the view's shortcode ( [wpv-view name="caf-view-of-topics" ids="2135"] ) directly on a test page ( Test page from TS support ) and it showed the 'No items found' message.
I checked the view "CAF View of Topics" and noticed that it was set to be ordered by one of the system custom field "toolset-post-sortorder".
( screenshot: hidden link )
As a result, the view's query would ignore any posts, which wouldn't have any value stored in this custom field and hence the no items found message.
I've changed the order settings to order by post title and the requested topic post is showing now.
Waqar, thank you so much! I think I believed sorting them in that order would sort them according to their order in the LearnDash course builder, but I'll find another way to do that.
Thank you again!
Saul
Waqar,
At the moment, and per your suggestion, the posts are being sorted according to their post title. I would like to order them based on the order of the post IDs passed to the "wpv-view" shortcode via the "ids" attribute. So if the shortcode is invoked as...
[wpv-view name="caf-view-of-topics" ids="3,1,2"]
...the display order of posts will be 3, 1, and 2.
Is that possible?
Thanks.
Saul
Thanks for writing back.
To make the view order the results by the IDs passed in the shortcode attribute, you can use the 'wpv_filter_query' filter:
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )
add_filter( 'wpv_filter_query', 'custom_view_order_func', 1000 , 3 );
function custom_view_order_func( $query_args, $view_settings ) {
// process if specific view
if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 1234) ) {
$query_args['orderby'] = 'post__in';
}
return $query_args;
}
Note: Please replace '1234' with your actual view's ID.
This is exactly what I needed. Thank you, Waqar!