• I have customized and styled the search results page search.php according to my needs and I implemented a main menu myself and check which menu items are active by comparing the URL get_permalink() of the current page with the given menu item.

    Strangely, on the search page, there is always one menu item active and get_the_ID(); returns an arbitrary page ID. Why is that? Shouldn’t the ID be false or that of the homepage?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    get_the_ID() returns the ID of the current post object in a loop. It’s intended only for use within the associated loop. Using it outside of the loop will have it return the last post ID of whatever loop had run previously. It only returns false if no loop at all had yet run. There can be loops run internally which you may not be aware of.

    Maybe you could use get_queried_object_id() instead? It returns 0 on search result pages.

    Thread Starter parallactic

    (@parallactic)

    I see. I didn’t realize that they were just loop variables. In that case, the behavior obviously makes sense and get_queried_object_id() is clearly the way to go. Thanks for the tip!

    • This reply was modified 2 years, 10 months ago by parallactic.
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Search page sets a global post’ is closed to new replies.