WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_queried_object no longer working with pre_get_posts and pretty permalinks (15 posts)

  1. jeich
    Member
    Posted 6 months ago #

    Hi!

    We are experiencing the category issue referenced here ->https://core.trac.wordpress.org/ticket/26627

    We applied '3.8.1' and the issue is still unresolved.

    The custom query is operable with the default permalink (?cat=) option set. Our mobile barcode scan now returns the homepage. Does anyone feel up to a challenge?

  2. bcworkz
    Member
    Posted 6 months ago #

    get_queried_object() is working correctly in a freshly installed 3.8.1 site in 'pre_get_posts'. What are you doing differently such that it is not working? Until we can see the same error you are seeing, it will be difficult to suggest a fix.

    I can't imagine why it wouldn't be applied, but you might confirm the changes shown here were applied to you installation.

  3. jeich
    Member
    Posted 6 months ago #

    Yes, I have the matching source code.

  4. jeich
    Member
    Posted 6 months ago #

    Hi!

    We are experiencing the category issue referenced here -> https://core.trac.wordpress.org/ticket/26627

    We applied '3.8.1' and the issue is still unresolved.

    The custom query is operable with the default permalink (?cat=) option set. The issue complicates category handling and is also a known issue. WordPress rolled out a 'fix' on the 23rd to address the issue, we applied '3.8.1' and the issue is still unresolved. The critical component being effected is the mobile barcode scan for subscribers, which is passed an incorrect variable after input.

    partner-functions.php
    query.php query.php

  5. bcworkz
    Member
    Posted 6 months ago #

    While your problem might have the same symptoms as Trac #26627, the actual underlying cause is elsewhere because the Trac issue is fixed.

    I don't have the software nor hardware to fully test your situation. I'm only going to be able to help you if you can describe a situation I can setup on my installation that will result in a wrong value returned by $query->get_queried_object().

  6. jeich
    Member
    Posted 6 months ago #

    If you were to look over the output generated by dump_query() and had maybe the conditional statement where post_type is changed for the category pages, you would be able to see the problem before 3.8.1 was applied. After 3.8.1 you would notice a change, the correct variables are queried on the category page from where the sidebar widget calls the query. The child sites, however generate a non-default 'post-name' url linking to the category page, the 'post-name' of the referring site is not being handled by the query and the taxonomy-relationships key returns all records from the database. I don't know for what reason, I am studying query.php and looking at a wall for answers.

  7. jeich
    Member
    Posted 6 months ago #

    When the 'Default' Permalinks are set the Custom Query changes post_type for category pages (in pre_get_posts with get_queried_object) and returns all! the records. When the 'Non-default' Permalinks are set the Query returns the default post_type, zero records and the 404.

    The same 'site name permalink structure' is handled for 'Default' and 'Non-default' queries. -> */wp-admin/network/site-settings.php?id=##

    'Non-default' Permalinks operate the mobile barcode scan and other operable, 'secondary' site queries, all set for post-name. 'Default' permalinks return all the records in the taxonomy relationships key variable every time and is for staging only.

    Do I sound like a robot yet? Thank you!

  8. bcworkz
    Member
    Posted 6 months ago #

    I don't fully understand some of your terminology, but it sounds like some improper interpretation of requests is going on in addition to the queried object varying by permalink setting. Let's focus on one thing at a time, the object varying by permalink setting seems crucial. You seem to be getting all or none, neither of which are correct.

    Please post a copy of all the query arguments used to build the varying query, including the values of any variables used. If that is difficult due to scattered components, dump $query->query_vars from 'pre_get_posts' and post that. Unless the code is brief, you should use pastebin.com and just provide the link here.

  9. jeich
    Member
    Posted 6 months ago #

  10. bcworkz
    Member
    Posted 6 months ago #

    Thanks for the information. It wasn't exactly what I was looking for, but it proved to be quite useful. First, how the query changes depending on permalinks being enabled. It first seemed impossibly mysterious. It's quite simple really. It has to do with $wp->parse_request(). With no permalinks, the parser has to use what is passed in the request. With permalinks, it can use rewrite rules to transform what is passed in the request into different query vars.

    Thus, the difference is sort of a red herring, we are essentially starting with different query vars under each condition. Neither is necessarily wrong, just different. Where things go wrong is how the query is formed with each set of query vars. To stay sane, I'm backtracking on my previous idea to focus on the different permalink forms. We need to pick one or the other and get it working. It doesn't matter what the other does, since we are not going to use it.

    I'm going to focus on permalinks enabled, as that is what I and most people use. If you normally have them disabled, similar debugging path is still followed, but the specific query vars you start with are different. With permalinks enabled, you get 404 errors, nothing is returned. Examining the query tells us why. It specifically wants a post with the name 'site-permalink' among other things, like post meta '_post_meta_key' must equal '1' for this post. If either of these are not true, nothing is returned. Not knowing how the query vars were established, I can't say how this query got to be this way. Maybe you recognize something here that will lead to a solution.

    The other thing happening is the setting of the post_type var to include 'partner' is not happening, probably because of what is returned by $wp_query->get_queried_object(). More than likely NULL is returned. This method continues to have issues even though the category issue was fixed. The inline docs list what sorts of queries the method works for: category, tag, taxonomy, posts page, single post, page, or author. About half of these do not work!

    The whole thing is a kludge really. It makes no sense to get a queried object in 'pre_get_posts' because we haven't queried anything yet, so how can we have an object? A bunch of work around code was put in place so developers using 'pre_get_posts' have something to work with in setting or unsetting query vars for certain conditions. The workaround code was to only apply for the most commonly used queries, more obscure queries are out of luck.

    Except we are now out of luck because the code provided doesn't even work half the time. I suggest you give up on $wp_query->get_queried_object() and get the values you need by other means. There's nothing magical about this method. You can get values needed just as well as it can. It's nice having everything in one place, but it's more efficient to only get what you need and forget the rest. For inspiration on how to get certain values, see the source code in query.php, line 3262.

    Keep in mind some of the techniques may not work, so you may need to find related alternatives just as the category patch did. I don't have a specific patch for $wp_query->get_queried_object(), but I intend to do more research and reopen the related Trac ticket. That is unless you would rather do it.

  11. jeich
    Member
    Posted 6 months ago #

    The query now varies /%category%/%postname%/ from the 'Custom Structure' field on the 'Site Permalinks' page. The site is functional.

    The get_queried_object function in query.php defines the category variable with added vigilance, had I realized /%category%/ could be defined next to /%postname%/ in the 'Custom Structure' field, this report may never have been written. The increased customization adds functionality with greater accuracy, I only hope the additional variable is the intended 'fix' and not a fluke. If you have ideas for improving query.php and require resources that were not provided in the solution, I will do everything in my power to help. Again, your ideas are welcome and thank you. -Jeich

  12. bcworkz
    Member
    Posted 6 months ago #

    I'm glad you were able to achieve functionality despite the partially malfunctioning get_queried_object(). I regret the confusion it caused, but I'm glad you brought it up, and persisted even though we thought the issue was fixed. It allows us to improve the WP core code.

    I've looked into this a little further, there are a few situations where the queried object just is not available anywhere in 'pre_get_posts', but for the most part, it can be composed one way or another, especially for the default no pretty permalinks case. Things are more difficult for pretty permalinks.

    I'm still unsure how this came to light in 3.8.1, the current code has been in place for quite some time (other than the recent partial "fix"). I suspect another process was composing queried objects somewhere, and is no longer doing so. I haven't figured out where that was yet, I've installed a 3.7 version for testing, but haven't yet had chance to actually investigate.

    I'm pretty sure I've gotten a handle on the situation. The information you provided was instrumental in my locating the root of the problem, so I am now able to induce all manner of errors at will.

  13. bcworkz
    Member
    Posted 6 months ago #

  14. Gregory Karpinsky (@tivnet)
    Member
    Posted 6 months ago #

    Having similar problem when calling is_category in pre_get_posts and the category does not exist (404 will follow).
    Added patch

    function is_category( $category = '' ) {
    ...
    $cat_obj = $this->get_queried_object();
    if ( is_null( $cat_obj ) ) {
    	return false;
    }
  15. KirLaide
    Member
    Posted 2 months ago #

    Can be used wp_reset_query(); before error

Reply

You must log in to post.

About this Topic