Support » Plugins » Hacks » Trouble using get_pages with pre_get_posts hook

  • Resolved jmaurer

    (@360zen)


    Hi there,
    I’m struggling to get the results I want here. After a couple hours of googling/forum scraping, I’ve decided it’s time for a new post.

    I’m trying to look at children of the current (queried) page, and if the page has a specific term within a specific taxonomy, show the child page content instead of the originally queried page. Here’s my code (via gist).

    This method actually works perfectly on the home page. The child page of “Home” is found, and the query is altered to show that page. However, I get no results when I try this on /about-us/mission/ for example (even though there is a child page that fits the conditions). If I var_dump($children), on the “Home” page, I get one result: the correct one. If I var_dump($children) on another page, I just get an array of ALL top level pages (and no children of anything). What am I doing wrong here? I’ve tried this without the ‘parent’ parameter AND without the ‘child_of’ parameter.

    If I var_dump($pid) on any page besides the home page, I receive “int(0)”. I imagine this is the root of the problem. Something about $query->query_vars['page_id'] is not yielding the actual ID of the current page. I don’t know if just haven’t found the correct combination of parameters, or if there is a fundamental flaw in what I’m attempting…

    Any help would be much appreciated.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter jmaurer

    (@360zen)

    Okay, I’ve solved this. I just needed to replace $query->query_vars['page_id'] with get_queried_object_id() and everything works!

    However, I just read a response from a different site and it appears that indeed, according to the codex, pre_get_posts is not supposed to work with pages. Weird though, because it IS working. I guess we’ll see if anything breaks…

    Moderator bcworkz

    (@bcworkz)

    FWIW, ‘pre_get_posts’ IS intended to work with pages. It works with any request that is destined for the “Loop”. Perhaps someone is confused by the “posts” part of the hook. As pages are just a particular post type, it still applies.

    You cannot believe everything you read on the Internet, including this post 🙂 Do your own good research and decide for yourself.

    Thread Starter jmaurer

    (@360zen)

    Ah okay. It’s good to know that my approach isn’t flawed. However, if that’s true, I’m a little confused by this portion of the Codex:

    pre_get_posts cannot be used to alter the query for Page requests (page templates) because ‘is_page’, ‘is_singular’, ‘pagename’ and other properties (depending if pretty permalinks are used) are already set by the parse_query() method. See: Query Overview. The recommended way to alter the main query for page requests is to use new WP_Query in the page template itself.

    Page in that context does not mean page post type it means a url request.

    Thread Starter jmaurer

    (@360zen)

    Okay cool. Good to know. Thanks for clearing that up. Marking as resolved.

    Moderator bcworkz

    (@bcworkz)

    For anyone still reading, it is possible to change is_page, is_singular etc. properties from within ‘pre_get_posts’ callbacks, so the above quoted portion of the Codex is a little misleading. I’ve re-written that segment so the tone is less imperative. Suggestions for further improvement are welcome. Or improve it directly yourself, anyone can edit the Codex.

    Thread Starter jmaurer

    (@360zen)

    Okay. I’ve got one last question on this topic (having trouble with this on the home page), but have asked it in a new thread. Any input would be much appreciated.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Trouble using get_pages with pre_get_posts hook’ is closed to new replies.