How to pass/get the ID of the page where the search is placed?

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 Ernest Marcinko 7 years, 4 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #11113
    onlinesid
    onlinesid
    Participant

    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,
    Sid

    #11119
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi!

    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 :)


    #11132
    onlinesid
    onlinesid
    Participant

    Hi,

    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,
    Sid

    #11158
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi 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:

    $args['_page_id']
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #11176
    onlinesid
    onlinesid
    Participant

    When can I get that new version? 🙂

    #11177
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    It will be released within the next 4 weeks 🙂

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #11180
    onlinesid
    onlinesid
    Participant

    Awesome! Thanks Ernest 🙂

    #11181
    Ernest Marcinko
    Ernest Marcinko
    Keymaster
    You cannot access this content. Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘How to pass/get the ID of the page where the search is placed?’ is closed to new replies.