Support » Fixing WordPress » WP_Query() returning different results depending on browser

  • This is REALLY strange!

    I’ve got a custom post type (named ‘my_custom_post_type’). When I try to query for a specific one of these custom posts I get different results depending on the browser I’m using, which makes no sense to me.

    My code is:

    $args = array (
    	'post_type' => 'my_custom_post_type',
    	'p' => 12,
    	'post_status' => array ('publish', 'pending')
    	) ;
    $query = new WP_Query ($args) ;

    If the post with post_type = ‘my_custom_post_type’ whose ID = 12 has status ‘publish’ or ‘pending’ it is found when I’m using Firefox (v17.0.1). However, if that post has status ‘pending’, then it is not found when I use IE (v8), Chrome (v23.0.1271.95 m), Safari (v5.1.7) and Opera (v11.62).

    I’m using WordPress v3.4.1 and the browsers are running on a Windows Vista SP2 machine (which is different from the machine WP is running on).

    I have verified that $query->request is IDENTICAL in all cases (see below), which is why this makes no sense to me!

    SELECT wp_posts.*
    FROM wp_posts
    	1=1 AND
    	wp_posts.ID = 12 AND
    	wp_posts.post_type = 'my_custom_post_type' AND
    	(wp_posts.post_status = 'publish' OR wp_posts.post_status = 'pending')
    ORDER BY wp_posts.post_date DESC

    Please help! This is for a site I’m developing for a client that is supposed to go-live next week and I need to fix this problem ASAP.

Viewing 15 replies - 1 through 15 (of 15 total)
  • What specifically are you seeing differently?

    A link would be nice too.

    $query->post_count is 1 when accessing with FF, 0 with the other browsers (i.e., the db query succeeds with FF and fails with the other browsers).

    Sorry, but the site isn’t public yet so I can’t provide a link.

    Can I see your register_post_type() call? Are you using any custom taxonomies?

    The register_post_type() call is as follows:

    create_post_types ()
    	register_post_type ('my_custom_post_type',
    		array (
    			'labels' => array (
    				'name' => __ ('My Name'),
    				'singular_name' => __ ('My Name'),
    				'all_items' => __ ('All My Names'),
    				'add_new_item' => __ ('Add New My Name'),
    				'edit_item' => __ ('Edit My Name'),
    				'new_item' => __ ('New My Name'),
    				'view_item' => __ ('View My Name'),
    				'search_items' => __ ('Search My Names'),
    				'not_found' => __ ('No my names found'),
    				'not_found_in_trash' => __ ('No my names found in Trash')
    			'public' => false,
    			'menu_position' => 5,
    			'has_archive' => false,
    			'supports' => array (
    				'title', 'editor', 'author', 'revisions', 'page-attributes'
    			'show_ui' => true
    		) ;
    add_action ('init', 'create_post_types') ;

    And yes, there is one custom taxonomy used with this custom post type, as follows:

    create_taxonomies ()
    	$labels = array (
    		'name' => _x ('My Name Types', 'taxonomy general name'),
    		'singular_name' => _x ('My Name Type', 'taxonomy singular name'),
    		'all_items' => __ ('All My Name Types'),
    		'parent_item' => null,
    		'parent_item_colon' => null,
    		'edit_item' => __ ('Edit My Name Type'),
    		'update_item' => __ ('Update My Name Type'),
    		'add_new_item' => __ ('Add New My Name Type'),
    		'new_item_name' => __ ('New My Name Type'),
    		'add_or_remove_items' => __ ('Add or remove my name types'),
    		'choose_from_most_used' => __ ('Choose from the most used my name types'),
    		'menu_name' => __ ('My Name Types'),
    		) ;
    	register_taxonomy ('my_custom_post_type-type', 'my_custom_post_type', array (
    		'hierarchical' => true,
    		'labels' => $labels,
    		'show_ui' => true,
    		'update_count_callback' => '_update_post_term_count',
    		'query_var' => true,
    		)) ;
    	return ;
    add_action ('init', 'create_taxonomies', 0) ;

    and just to be clear: the problem isn’t how the site looks when viewed in different browsers. It is that WP_Query() is failing to find the custom post when I access the site in any browser other than FF.

    In case it helps, here are the relevant rows from the various wp tables (with irrelevant columns left out for brevity).


    ID	post_author	post_date		post_title	post_status	post_name	post_type
    12	18		2012-11-12 19:21:19	My title	pending		my-title	my_custom_post_type


    term_id	name	slug
    26	Name	name


    term_taxonomy_id	term_id	taxonomy
    47			26	my_custom_post_type-type


    object_id	term_taxomomy_id	term_order
    12		47			0

    (hopefully, the above formatted correctly, without a “preview” I won’t know until I post this)

    Hi Paul,

    I don’t see a namespace listed when you called translate(). Any reason why? Are you actually translating?

    Have you tried it without the translation functions running?

    sorry, the above code is “sanitized” to remove references to my client’s name and I just messed up the sanitation. But since the theme I’m developing is just for this one client and I18N is not really necessary for them, I just went ahead and removed all __() and _x() just to see if somehow the translation could be the problem, but no such luck.

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    Are you using any form of “caching” plugin?

    Did you try to clear the cache of it?

    Did you try disabling it?

    Also, are you “logged in” to the site on the Firefox browser but not on the other browsers?

    no caching plugins.

    but, yes, I have been logged in while using FF and not the others (FF being my preferred browser…the others I use to check x-browser compat issues). And I just verified that if I log out of FF then the query fails and when I log in with Chrome the query succeeds! So, we’re closer to a solution, thanx!

    What effect could that possibly have? I have no code wrapped in is_user_logged_in() and the only code I have wrapped in is_admin() creates custom meta boxes for this custom post type (and another custom meta box for post_type=’post’ that attaches the ID of one of these custom posts to a regular post).

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    Ahh. Okay.

    Yes, “Pending” posts are not considered published yet, and so only logged in users with the proper role and capabilities to see them are allowed to see them in normal queries.

    @samuel: I understand that for the normal loop, pending posts aren’t included. But this is happening outside of the loop. My the args I’m passing to WP_Query() explicitly ask for posts with status ‘pending’. Can you point me to where this is documented?

    Looking into the code of WP_Query()->get_posts(), I see that there is some code that checks is_user_logged_in() and current_user_can() while building certain queries. However, those code branches aren’t active in this case and the resulting $query->request that is being constructed is the same whether I’m logged in or not (see the 2nd code block in my OP). I’ve even verified that wpdb->query() is getting the correct query which it directly passes to the PHP builtin mysql_query(), but that mysql_query() fails to find the post with pending status when I’m not logged in.

    So, while you’ve helped me get closer to a solution, I’m still really perplexed, because the PHP builtin mysql_query() function can’t possibly know whether the user is logged into WP, can it?

    after @samuel‘s fist comment, I also did a little poking around and found register_post_status(). So, I registered a custom status with ‘public’ => true, however, that status doesn’t show up in the editor and so I can’t assign it to my custom post. A little more poking around on these support forums and it looks like while the ability to add custom post statuses was added, the patches to allow them to be assigned when editing a post hasn’t made it into WP yet 🙁

    so I think, for the time being, I’m just going to add a custom meta to these custom posts and use that for the “inactive” status that I had been using ‘pending’ for, leaving all my custom posts with status ‘publish’ for now.

    but I would still really like to understand more fully why this is not working 🙁

    actually, I just added a new taxonomy, that was easier.

    so, I’m able to move forward with my client’s project, but I’m not yet marking this as resolved, because I still don’t understand why being logged in or not to WP would affect the results returned by PHP’s builtin mysql_query().

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘WP_Query() returning different results depending on browser’ is closed to new replies.