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 has 7 replies, 2 voices, and was last updated 9 years, 6 months ago by
Ernest Marcinko.
-
AuthorPosts
-
December 4, 2016 at 10:06 pm #11113
onlinesid
ParticipantHi,
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:
[code]
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;
}
[/code]Currently how can I achieve something like the above?
Kind regards,
SidDecember 5, 2016 at 12:08 pm #11119Ernest Marcinko
KeymasterHi!
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: http://i.imgur.com/Rnuslom.png
December 5, 2016 at 9:00 pm #11132onlinesid
ParticipantHi,
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 #11158Ernest Marcinko
KeymasterHi 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:
[php]$args[‘_page_id’][/php]
December 6, 2016 at 12:10 pm #11176onlinesid
ParticipantWhen can I get that new version? 🙂
December 6, 2016 at 12:11 pm #11177Ernest Marcinko
KeymasterIt will be released within the next 4 weeks 🙂
December 6, 2016 at 12:31 pm #11180onlinesid
ParticipantAwesome! Thanks Ernest 🙂
December 6, 2016 at 12:32 pm #11181Ernest Marcinko
KeymasterYou cannot access this content.
-
AuthorPosts
- The topic ‘How to pass/get the ID of the page where the search is placed?’ is closed to new replies.