$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Unable to retrieve any posts of CPT in wp-admin|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Unable to retrieve any posts of CPT in wp-admin

matteradmin11PV0评论

I have a site with a few custom post types. Those are all registered in a very similar fashion sharing most attributes:

const ARGS = [
    'hierarchical'          => true,
    //'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => true,
    'exclude_from_search'   => false,
    //'publicly_queryable'    => true,
    'capability_type'       => 'page',
    'show_in_rest'          => false,
];

For example the CPTs order and person are registered like this

private static function register_order()
{
    $args = array_merge(
        [
            'label'                 => __( 'order', 'X' ),
            'labels'                => [...],
            'supports'              => ['title', 'editor', 'author'],
        ],
        self::ARGS
     );

    register_post_type( 'order', $args );
}

private static function register_person()
{
    $args = array_merge(
        [
            'label'                 => __( 'person', 'X' ),
            'labels'                => [...],
            'supports'              => ['title', 'author'],
        ],
        self::ARGS
    );

    register_post_type( 'person', $args );
}

However, when in the wp-admin, I currently only see persons whereas orders gets me an empty list.

Debugging this with the Debug Bar and Query Monitor, I get the following main query for order (redacted the post_status for both queries)

SELECT wp_posts.ID, wp_posts.post_parent
FROM wp_posts 
WHERE 1=1 
AND (wp_posts.ID = '0')
AND wp_posts.post_type = 'order'
AND (...) 
ORDER BY wp_posts.menu_order ASC, wp_posts.post_title ASC

And the query for person is

SELECT wp_posts.ID, wp_posts.post_parent
FROM wp_posts 
WHERE 1=1 
AND wp_posts.post_type = 'person'
AND (...) 
ORDER BY wp_posts.menu_order ASC, wp_posts.post_title ASC

Now I understand SQL good enough to know that this line

AND (wp_posts.ID = '0')

is the reason I don't get any results. I just don't have any idea why it is there. I don't have any pre_get_posts hook set, deactivated all plugins except this one, no change whatsoever.

I have a site with a few custom post types. Those are all registered in a very similar fashion sharing most attributes:

const ARGS = [
    'hierarchical'          => true,
    //'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => true,
    'exclude_from_search'   => false,
    //'publicly_queryable'    => true,
    'capability_type'       => 'page',
    'show_in_rest'          => false,
];

For example the CPTs order and person are registered like this

private static function register_order()
{
    $args = array_merge(
        [
            'label'                 => __( 'order', 'X' ),
            'labels'                => [...],
            'supports'              => ['title', 'editor', 'author'],
        ],
        self::ARGS
     );

    register_post_type( 'order', $args );
}

private static function register_person()
{
    $args = array_merge(
        [
            'label'                 => __( 'person', 'X' ),
            'labels'                => [...],
            'supports'              => ['title', 'author'],
        ],
        self::ARGS
    );

    register_post_type( 'person', $args );
}

However, when in the wp-admin, I currently only see persons whereas orders gets me an empty list.

Debugging this with the Debug Bar and Query Monitor, I get the following main query for order (redacted the post_status for both queries)

SELECT wp_posts.ID, wp_posts.post_parent
FROM wp_posts 
WHERE 1=1 
AND (wp_posts.ID = '0')
AND wp_posts.post_type = 'order'
AND (...) 
ORDER BY wp_posts.menu_order ASC, wp_posts.post_title ASC

And the query for person is

SELECT wp_posts.ID, wp_posts.post_parent
FROM wp_posts 
WHERE 1=1 
AND wp_posts.post_type = 'person'
AND (...) 
ORDER BY wp_posts.menu_order ASC, wp_posts.post_title ASC

Now I understand SQL good enough to know that this line

AND (wp_posts.ID = '0')

is the reason I don't get any results. I just don't have any idea why it is there. I don't have any pre_get_posts hook set, deactivated all plugins except this one, no change whatsoever.

Share Improve this question asked Jun 27, 2018 at 11:10 kerokero 6,3501 gold badge25 silver badges34 bronze badges 4
  • Are you using pre_get_posts? Note that filter is for all queries, not just frontend queries, you have to check for the main query, and you have to explicitly check for admin vs frontend, or it'll also be applied to all your admin post screens – Tom J Nowell Commented Jun 27, 2018 at 11:52
  • @TomJNowell No. The installation is yet very basic: ACF (which I disabled, nothing changed) and my custom plugin. Which right now only adds the CPTs and REST endpoints (the latter I disabled, nothing changed) – kero Commented Jun 27, 2018 at 12:17
  • 3 order is in the list of CPT that you cannot use : codex.wordpress/Function_Reference/register_post_type – mmm Commented Jun 27, 2018 at 12:26
  • @mmm Thanks alot! Please write it up as an answer. Why does the new docu not show vital info such as this? – kero Commented Jun 27, 2018 at 12:49
Add a comment  | 

1 Answer 1

Reset to default 1

As mmm pointed out in the comments, order should not be used as a name for a custom post type, as it may "interfere with other WordPress functions". Read more in the Codex:

Reserved Post Types The following post types are reserved and used by WordPress already.

  • post
  • page
  • attachment
  • revision
  • nav_menu_item
  • custom_css
  • customize_changeset

In addition, the following post types should not be used as they interfere with other WordPress functions.

  • action
  • author
  • order
  • theme
Post a comment

comment list (0)

  1. No comments so far