WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 20 months ago

Last modified 20 months ago

#16641 closed defect (bug) (wontfix)

custom post has_archive in multisite

Reported by: andreamk Owned by:
Milestone: Priority: normal
Severity: minor Version: 3.1
Component: Posts, Post Types Keywords: reporter-feedback
Focuses: Cc:

Description

has_archive flag in register_post_type only works on the main site

in the subsite the error is
Warning: Illegal offset type in isset or empty in .../wp-includes/post.php on line 812

An the template redirect ad the file archive-{$post_type}.php don't work.

I temporarily fixed by changing the function get_post_type_object

function get_post_type_object( $post_type ) {
	global $wp_post_types;
	
	if (is_array($post_type)) {
		$post_type = $post_type[0];
	}

	if (empty($wp_post_types[$post_type]) )
		return null;

	return $wp_post_types[$post_type];
}

and using is_post_type_archive in file archive.php

<?php 

if (is_post_type_archive('my_post_type')) {
require_once ('archive-my_post_type');
return;
}

.
.
.

saw my bad English I struggle to explain better.

Change History (6)

comment:1 @andreamk4 years ago

  • Cc andleo75@… added
  • Severity changed from major to minor

update
I found the problem

has_arcive adds the custom post at wp_query type obj
this was in conflict with my function because my registration was the same thing.

So in query_var post_type array was 2 times my custom_post

I think it's a good idea to add a control to function parse_query to avoid this error but is a minor bug.

comment:2 @nacin4 years ago

Could you show the code you were using? Really not sure what you're trying to do here.

comment:3 @nacin4 years ago

  • Keywords reporter-feedback added

comment:4 follow-up: @andreamk4 years ago

Suppose you create a custom post events and register it with has_archive.

if I put in my plugin a a new action

add_action('pre_get_posts', 'addCustomPost' );

public function addCustomPost( $query ) {
    if (!is_admin() && !is_single() && !is_feed()) {
	$exclude = array('attachment','revision','nav_menu_item');
	$pType = $query->get('post_type');

	if (is_array($pType)) {
	        foreach ($pType as $p) {
		 if (in_array($p, $exclude)) {
			return $query;
		 }
	        }
	} else {
		if (in_array($pType , $exclude)) {
			return $query;
		}
	}
        if (is_array($query->query_vars['post_type']) {
              $query->query_vars['post_type'][] = 'events';
        } else {
              if (!empty($query->query_vars['post_type'])) {
                   $query->query_vars['post_type'] = array($query->query_vars['post_type'],'events);
              } else {
                   $query->query_vars['post_type'] = 'events';
              }
        }
    }
}

now if you go in events archive the var query_var become

object(WP_Query)#4322 (44) {
  ["query_vars"]=>
  array(57) {
    ["post_type"]=>
    array(2) {
      [0]=>
      string(7) "events"
      [1]=>
      string(7) "events"
    }
.
.
.

to prevent this error I must check if my custom post type isn't already in query var.
is the right thing to do but with all the plugins that there are possibilities of conflict are high.
I think that preventing this type of error strengthens wordpress

comment:5 in reply to: ↑ 4 @ericmann20 months ago

  • Resolution set to wontfix
  • Status changed from new to closed

Replying to andreamk:

to prevent this error I must check if my custom post type isn't already in query var.

Correct. Otherwise you're just blindly adding to an array. It's an easy mistake to make, but something you should really be insulating yourself from in your code.

is the right thing to do but with all the plugins that there are possibilities of conflict are high.
I think that preventing this type of error strengthens wordpress

WordPress can't protect against every kind of mistake plugin developers can make. Yes, there is a possibility of conflicting post types and conflicting query variables, which is why it's best you protect your own code from conflicts.

comment:6 @helen20 months ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.