powerbuoy
powerbuoy

Reputation: 12838

Adding custom post type programmatically doesn't add it to the admin

I have a super strange problem where if I add a custom post type programmatically it refuses to show up in the admin.

This works fine:

    <?php
    wp_insert_post(array(
        'post_title' => 'TEST', 
        'post_content' => 'TESTING'
    ));

And correctly adds a new post to the posts page in the admin.

This, however, behaves very strange:

    <?php
    wp_insert_post(array(
        'post_type' => 'items', 
        'post_title' => 'TEST', 
        'post_content' => 'TESTING'
    ));

After running the function, if I visit the items-page in the admin the new post will not show up. However, the "number of posts" counter does increase and if I manually enter the ID of the post in the URL I can edit it properly. Saving it again does not resolve the issue.

As you can see in the screenshot there's a total of 68 items (this is correct) but only 4 of them show up in the list. The 12 drafts are also added by code and if I switch to the "Drafts"-tab it's completely empty.

I'm logged in as admin.

Missing Items

Upvotes: 0

Views: 1062

Answers (1)

powerbuoy
powerbuoy

Reputation: 12838

Silly me. I've been using pre_get_posts to modify WP's search results (allowing users to sort on different fields for one), but I forgot to make sure it doesn't run on admin pages.

So on admin listings it would still try to sort on a custom field that only some posts have, thus every post lacking the custom field would not show up.

Hope this helps someone in the future.

Upvotes: 1

Related Questions