Opened 2 years ago

Last modified 19 months ago

#16949 new defect (bug)

Custom post types: error on page if query contains multiple post types

Reported by: mastermind Owned by:
Priority: normal Milestone: Future Release
Component: General Version: 3.1
Severity: normal Keywords: has-patch 2nd-opinion
Cc:

Description

I'm using custom post types, and I modified the $query to include multiple post types. This works quite well, until I enter a URL to a non-existant post (modifying the last part, i.e. the post's slug).

// some HOWTO linked from the Codex suggests this,
// can't find the link right now

function add_posttypes_to_query($query)
{
	if (!is_admin() && !$query->query_vars['suppress_filters'])
	{
		if (!is_home())
			$post_type = array('video', 'post');

		$query->set('post_type', $post_type);
	}

	return $query;
}

add_filter('pre_get_posts', 'add_posttypes_to_query');

The error I get is: “Warning: Illegal offset type in isset or empty in /home/lxg/www/shops.supreme.de/supremeshops/branches/private_wp3/wp-includes/post.php on line 811”.

It appears that the function get_post_type_object gets the array passed if no posts were found.

Also, I realized that custom post single pages don't issue a HTTP 404 status on errors. But this may be due to local modifications. I am currently not able to set up an environment where I isolate this problem.

Attachments (2)

16949.diff (498 bytes) - added by aaroncampbell 2 years ago.
Don't attempt old slug redirect for multi-post_type queries
16949.2.diff (1.6 KB) - added by aaroncampbell 2 years ago.
Allow wp_old_slug_redirect() to handle multiple post types

Download all attachments as: .zip

Change History (13)

  • Milestone Awaiting Review deleted
  • Version set to 3.1

There's a few get_post_type_object() calls in query.php. At least one might allow an array to slip through.

Can anyone track this down? This looks like a good bug to fix.

It looks like this is caused by the call to is_post_type_hierarchical() in wp_old_slug_redirect(). It just passes through what it gets from get_query_var('post_type') which can be an array. The check was added in [16818] and [16819] to fix #15140

It seems like we should just check for an array, loop through, and return if ANY of them is hierarchical? I'll test it an put up a patch in a few minutes.

  • Milestone set to 3.2

3.1 regression, 3.2 for now.

Don't attempt old slug redirect for multi-post_type queries

Allow wp_old_slug_redirect() to handle multiple post types

The problem turned out to be that wp_old_slug_redirect() expects post_type to be a string. It not only passes it straight to is_post_type_hierarchical() but a couple lines later it pushes into a query using $wpdb->prepare(). This leaves two options: Upgrade wp_old_slug_redirect() to handle multiple post types, or bail when we see we have multiple post types.

As nacin pointed out in chat " One might argue that you can't guess which page to redirect to if it could be one of a few post types". It seems to me that our guess would be less accurate, but we still might be able to do it. Both options have their advantages.

Not guessing would open things up for whoever is adding the additional post types to the query to do the guessing themselves. A little less "magical" but pretty flexible. This is attached as 16949.diff

If we want more magic we could actually force post_type to an array, loop through and generate the post_type= bits of the query from the non-heirarchical post_types, which creates a query like this:

SELECT post_id FROM wp_postmeta, wp_posts WHERE ID = post_id AND ( post_type = 'somepostype' OR post_type = 'anotherposttype' ) AND meta_key = '_wp_old_slug' AND meta_value = 'bad-slug'

This is attached as 16949.2.diff

  • Keywords has-patch 2nd-opinion added

comment:6   ryan2 years ago

Fine with 16949.diff for 3.2. The other can be considered for another day.

Per IRC: Small patch now, big one in 3.3. One fixes a bug, one adds support for something that's never worked.

comment:8   ryan2 years ago

In [18330]:

Don't attempt old slug redirect for multi-post_type queries. Props aaroncampbell. see #16949

comment:9   ryan2 years ago

  • Milestone changed from 3.2 to Future Release
  • Milestone changed from Future Release to 3.3
  • Milestone changed from 3.3 to Future Release

Punting the remainder.

Note: See TracTickets for help on using tickets.