Forums › Forums › Search & Filter Pro › Custom query on AJAX loads
Tagged: ajax, custom query, meta field
- This topic has 5 replies, 3 voices, and was last updated 6 years, 6 months ago by Anonymous.
-
Anonymous(Private) April 12, 2018 at 10:28 am #171757
Hi!
First off, great plugin!
I have a question regarding the AJAX query calls. So, my results page has an option to filter posts based on authors. Every post has the option (checkbox through ACF plugin) for anonymous author (meta field – boolean). When a user lands on the unfiltered results page I would like to show all the posts (regardless of the anonymous meta field), but once a user filters based on particular author, I would like the results page not to show posts that have the meta field for anonymous author set. Is that possible?I’ve been trying to filter results with sf_edit_query_args filter, but as far as I understand that only works on page reload (and doesn’t get called when an AJAX call is made).
Thank you for your answer!
DomenAnonymous(Private) April 12, 2018 at 5:51 pm #171866Sure 🙂
1. Yes. All the authors are listed in the dropdown (independent of their blog posting anonymity). If the user chooses an author that only has anonymous posts, then the results should be empty. If the user chooses an author that has 3 posts published and one of them has them set as an anonymous author, then the results should show only 2 posts.
2. No, the authors are going to be added and removed.
3. Yes, I have it. But I modified the query to show only posts that don’t have the anonymous meta field set:function d_pre_get_posts( $query ){
if (is_author()) {
set_query_var( ‘meta_key’, ‘anonimni_avtor’ );
set_query_var( ‘meta_value’, 1 );
set_query_var( ‘meta_compare’, ‘!=’ );
}
}
add_action( ‘pre_get_posts’, __NAMESPACE__ . ‘\\d_pre_get_posts’ );Ross Moderator(Private) April 12, 2018 at 7:26 pm #171898Hi Domen
It looks like the problem is your usage of
sf_edit_query_args
The reason for the filter, is so that it works effectively for all our queries and ajax.
One of the issues I see with your code is actually with this line:
if (is_author()) {
A typical ajax request will be performed at
wp-admin/admin-ajax.php
, the functionis_author
will always return false, this is applicable when using our filtersf_edit_query_args
and your display method is set toshortcode
I added an extra line in our example to show how to set query arg properties using our function – https://www.designsandcode.com/documentation/search-filter-pro/action-filter-reference/#Edit_Query_Arguments
If you wanted
is_author
to work correctly, you would need to use the display methodarchive
where our ajax requests are loaded from the same page.Hope that makes sense
Thanks
Anonymous(Private) May 20, 2018 at 5:08 pm #178610Hi,
thank you Ross for your answer. The project was kind of on hold for a while, so I’m only replying now.
I’ve played a bit withsf_edit_query_args
and got it working.
So my setting was as follows:I have a home.php template set up, containing:
<?php echo do_shortcode('[searchandfilter id="' . BLOG_FILTER . '"]'); ?> <?php echo do_shortcode('[searchandfilter id="' . BLOG_FILTER . '" show="results"]'); ?>
I also have a page named Blog that is set up as the Posts Page on /wp-admin/options-reading.php
In backend I have a filter whose Display settings are:
Display results method: Using a Shortcode
Results URL: http://website.com/blog
Load results using Ajax? Checked
Make searches bookmarkable? CheckedI then have a filter (usually put in functions.php):
/** * Modify Search & Filter PRO plugin's query so that it returns posts of authors that are not anonymous. */ function domain_sf_filter_query_args($query_args, $sfid) { if ($sfid == 95 && isset($_GET['authors'])) { $query_args['meta_key'] = 'anonimni_avtor'; $query_args['meta_value'] = 1; $query_args['meta_compare'] = '!='; } return $query_args; } add_filter('sf_edit_query_args', 'domain_sf_filter_query_args', 10, 2);
So, when visiting an unfiltered page it returns all posts, but when filtering through authors, it returns only posts from that author that are not marked as anonymous author.
Thank you all for your help 🙂
Have a great start of next week!
Domen
-
AuthorPosts