Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › How to pass/get the ID of the page where the search is placed?
This topic contains 7 replies, has 2 voices, and was last updated by Ernest Marcinko 7 years, 4 months ago.
- AuthorPosts
- December 4, 2016 at 10:06 pm #11113
Hi,
I have been trying to do this all day yesterday, checked the knowledge base and documentation but there doesn’t seem to be a way to do this.
I have pages with category of ‘City’ (I used a plugin to enable categories for pages) and when in that page I want the search to only return results related to that page ID. So then I have to somehow pass the post ID of that page to the search.
At the moment I can do it by injecting the post ID via JavaScript after the page load, but I’m thinking that can easily break in the future if you update the html input IDs/names.
Ideally if the post ID of where the search took place is passed to $args so I can detect it from my own plugin:
add_filter("asp_query_args", "my_asp_query_args_change", 1, 2); function my_asp_query_args_change($args, $search_id) { $my_page_ID = $args['post_id']; // <-- this would be ideal // then we can do things like: $args['post_meta_filter'] = [ 'key' => 'my_custom_post_meta', 'value' => $my_page_ID 'operator' => '=', ]; return $args; }
Currently how can I achieve something like the above?
Kind regards,
SidDecember 5, 2016 at 12:08 pm #11119Hi!
If I’m guessing correctly you only want to return the pages from the same category as the currently displayed page is in.
In that case something like this could do the trick:
This will get the current post/page categories and push them to the query arguments.
In case you have posts/pages which are uncategorized, you might want to turn OFF an option to exclude them as well: https://i.imgur.com/Rnuslom.png
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
December 5, 2016 at 9:00 pm #11132Hi,
The problem is that get_the_ID() does not work from asp_query_args filter. It is always returning false. This is because it’s called via ajax so it lost the ID. That’s why I think it would be a massive improvement to your product if the post ID can be passed to the $args in asp_query_args filter.
Kind regards,
SidDecember 6, 2016 at 10:56 am #11158Hi Sid,
You are correct, I completely forgot about that, sorry.
I’ve already noted this, and will be adding another parameter to contain the page ID of which the ajax request was initiated. It’s going to be stored in the following argument in plugin version 4.10:
Best,$args['_page_id']
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
December 6, 2016 at 12:10 pm #11176When can I get that new version? 🙂
December 6, 2016 at 12:11 pm #11177It will be released within the next 4 weeks 🙂
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
December 6, 2016 at 12:31 pm #11180Awesome! Thanks Ernest 🙂
December 6, 2016 at 12:32 pm #11181You cannot access this content. Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
- AuthorPosts
The topic ‘How to pass/get the ID of the page where the search is placed?’ is closed to new replies.