Skip Navigation

[Resolved] View not returning the correct results

This support ticket is created 4 years, 2 months 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9: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: Africa/Casablanca (GMT+01:00)

This topic contains 12 replies, has 2 voices.

Last updated by Dan Kitsmiller 4 years, 1 month ago.

Assisted by: Jamal.

Author
Posts
#1764381

Hi Jamal, I tried your suggestion, but although the view does reload via ajax, it is still not displaying the correct posts. The view I am trying to refresh queries post IDs set by a shortcode attribute, using code that another member of your support team gave me. That support ticket is here: https://toolset.com/forums/topic/conditional-display-posts-based-on-custom-user-meta/

Here is a link to the page: hidden link

I have left the search forms visible for now. My goal is to enable the user to switch between the two views without leaving the page, so they don't need to select a price range every time they view the list. If you would like access to the site, just let me know and I will do a quick backup and send you a login. Thanks!

#1764385

I am not really sure to understand the issue of this view. Can you provide more details? What results are missing from the view? Or what results are present that should not?

I set your next reply as private to let you share credentials to your website, that would help me understand the situation quickly. ** Make a database backup before sharing credentials. **

#1766057

Thank you Dan, this explains the case very well.

Because you want to refresh the view using AJAX, the solution to pass the favorites posts using a shortcode attribute is not good. The shortcode will require that the page reloads.
Instead of passing the favorites post in a shortcode attribute, I'll suggest using the view's query filter hook wpv_filter_query. The following code should work, but I did not test it.
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters

add_filter( 'wpv_filter_query', 'pass_favorites_posts_to_view_3539' );
 
function pass_favorites_posts_to_view_3539( $query_args, $view_settings, $view_id ) {
	
    if ( $view_id == 3539 ) { // this is the view id
		$query_args['post__in '] = get_user_favorites(get_current_user_id(), $site_id = null, $filters = null);
	}
	
	return $query_args;
}

Update the queue to no more filter with the ids passed in the shortcode attribute.

Then you can refresh the view by triggering a click event on the filter submit button, which you can hide with CSS.

I hope this helps. Let me know if you still need further assistance with this.

#1766563

I removed the shortcode attributes filter added your code, but when I activate the snippit it throws a critical error, and I'm not sure why...

#1766751

I do not see why this snippet will trigger any errors. From Toolset settings, the snippet is tested with success. Check this screenshot hidden link

So, I updated it a bit and removed the unknown variables($site_id, $filters) but that should not be the issue.

Then I activated the snippet and visited the frontend and there were no issues. The snippet is again deactivated.

I also did not find any errors on the debug.log file hidden link, maybe you are using a different logging file. I could not verify, because the FTP access is not working for me.

Can you elaborate more on the critical errors triggered?

Can we also test on a new page with a new view, then we'll implement the same solution for the current view. The test will be as so:
- Create a new page and a new view to display the favorites.
- Click on the heart icon in another tab to change the current favorites.
- Return back to the new page and click on the view submit button to trigger an AJAX call
=> Check if the results are correct.

#1767007
Screen Shot 2020-09-05 at 12.52.32 PM.png

Hi Jamal, that's very strange. Every time I activate the snippet (favs-filter) and visit the front end, the site is broken-- every page, not just the map page. Also, when I visit the debug.log file that you reference, I do see fatal PHP errors, so I don't know that creating a new view at this point would be helpful. I can also confirm that the FTP credentials I sent earlier are correct (I'm logged in now with them), but there is an extra backslash in the initial folder path should be "buynewhomes.co/home/..." not "buynewhomes.co//home/..." The site does use SFTP. Can you try activating the snippet again and refreshing the map page? Thanks again for helping me get to the bottom of this 🙂

#1767017

One more thing that may be helpful... the query shortcode attribute I was using seems to retrieve the array of "Favorite" post IDs differently than your code does. Another member of your support team helped me with this here: https://toolset.com/forums/topic/conditional-display-posts-based-on-custom-user-meta/

#1769285

Thank you for your feedback. I think that FTP is not active on your website. Now, that I tried with SFTP it worked. I was able to activate PHP debugging and I got the following error hidden link

My snippet has an issue, it should pass 3 arguments to the function instead of one, I fixed it by correcting the following line:

add_filter( 'wpv_filter_query', 'pass_favorites_posts_to_view_3539', 10, 3 );

I fixed that, but, I am not sure if the view for favorites is working correctly, it also displays non-favorited posts, check this screenshot hidden link

We should probably fix it, before trying to fix the AJAX refresh part.

I activated the Toolset debug mode(Toolset->Settings->Fronte-end Content) to investigate this, but it failed because of the memory. I even tried it in a new page, check this screenshot hidden link

Maybe, if we deactivate all the plugins, except Toolset ones, and we switch to a default theme, the memory would be enough for us to investigate the issue. So, I suggest that we continue on a staging site, or that you allow me to take a copy of your website and investigate this locally. What would you prefer?

#1783505

Hi Jamal, I apologize for this delayed reply, as I was temporarily pulled to another project. I have cloned the site to a different server for you. It is at hidden link and your login credentials are the same. If you need ftp access I can get that to you. Thanks again for your help!

#1787329

Thank you for this test site. I see that you already added the custom code, but that does not affect the view. The favorites view still displays all results, check it here alone hidden link

I try to activate Views debugging in Toolset->Settings, but it is not working. So, I suspect that a cache object on your server-level is still returning cached data instead of the latest one.

I would like to take a copy of your website and check it on my local environment, would that be ok for you?

#1789585

Thanks Jamal. Yes, please feel free to clone the site as needed. Turning on Toolset debugging always generates memory errors when this map is displayed. Thanks again for your help!

#1790359

The issue was really a silly one. Notice the additional space after post__in $query_args['post__in '].

So, I guess, what remains, is to make the "Favorites Map - Extended" refresh using AJAX. I already added a class "my-ajax-refresh" to the submit button, I confirmed on the browser console that the following code will refresh the view and get any new favorited or unfavorited posts:

jQuery('.btn-secondary.my-ajax-refresh').click()

I'll let you integrate the Javascript code to your use case which involves Elementor.

Let me know if you will need further assistance.

#1790919

Thank you! I've been staring at this code forever trying to find the problem and totally missed this! My issue is resolved now. Thank you!