1

As I'm working on a private and custom theme for my website, I created custom post-types for different kinds of data i want to display (and adding extra data with custom fields).

Anyways, as I managed to get everything to work mostly, somehow the items disappeared from the admin menu, so I can't edit or add new content for my custom types.

Here's the source on pastebin: http://pastebin.com/dmA6PQSR

if (!function_exists("pre_posttype")):
function pre_posttype() {
    $types = array(
        "pre_selection" => array(
            "single" => "Selection",
            "description" => "A Selection of items which will appear on the front-page",
            "public" => false,
            "publicly_queryable" => true,
            "show_ui" => true,
            "show_in_nav_menus" => false,
            "show_in_menu" => true,
            "show_in_admin_bar" => false,
            "menu_position" => 30,
            "menu_icon" => "dashicons-visibility",
            "hierachical" => false,
            "supports" => array("title", "editor")
        ),
        "pre_project" => array(
            "single" => "Project",
            "description" => "(Programming-) Projects which this Website shows",
            "public" => true,
            "show_ui" => true,
            "show_in_nav_menus" => true,
            "show_in_menu" => true,
            "show_in_admin_bar" => true,
            "menu_position" => 31,
            "menu_icon" => "dashicons-category",
            "hierachical" => false,
            "supports" => array("title", "editor", "thumbnail")
        ),
        "pre_template" => array(
            "single" => "Template",
            "description" => "Template-Project for streaming",
            "public" => true,
            "show_ui" => true,
            "show_in_nav_menus" => true,
            "show_in_menu" => true,
            "show_in_admin_bar" => true,
            "menu_position" => 32,
            "hierachical" => false,
            "supports" => array("title", "editor")
        )
    );

    foreach ($types as $name => $t) {
        # Saving SINGLE and MULTI in variables and unsetting them, to avoid problems since we merge and post it
        # When neither is set, we'll create them from the name
        $s = isset($t["single"]) ? $t["single"] : (ucfirst(strtolower(strpos($name, "_") ? substr(strpos($name, "_")+1) : $name)));
        $m = isset($t["multi"]) ? $t["multi"] : (strcasecmp($s{strlen($s)-1},"s") ? $s : "{$s}s");
        # Un-setting extra values so they won't show up on the wp-registering and may screw shit up.
        unset($t["multi"], $t["single"]);

        register_post_type($name, PreLib::merge_all($t, array(
            # Defaults which will be applied when nothing is specified
            "label" => $m,
            "labels" => array(
                "name"                  => $m,
                "singular_name"         => $s,
                "add_new"               => "Add New",
                "add_new_item"          => "Add new {$s}",
                "edit_item"             => "Edit {$s}",
                "new_item"              => "New {$s}",
                "view_item"             => "View {$s}",
                "search_items"          => "Search {$s}",
                "not_found"             => "Nothing Found",
                "not_found_in_trash"    => "Nothing found in Trash",
                "parent_item_colon"     => "Parent {$s}",
                "all_items"             => "All {$m}",
                "archives"              => "{$s} Archives",
                "insert_into_item"      => "Insert into {$s}",
                "uploaded_to_this_item" => "Uploaded to this {$s}",
                "menu_name"             => $m,
                "update_item"           => "Update {$s}",
            ),
            "capability_type" => array(
                strtolower($s),
                strtolower($m)
            ),
            "rewrite" => array(
                "slug" => strtolower($s)
            ),
            "query_var" => $s,
            "rest_base" => strtolower($s)
        )));
    }
}
endif;
add_action("init", "pre_posttype");

public static function merge_all() {
       # If there are less than 2 arrays we can't merge
       if (func_num_args()  2) {
           # Iterate through all subs and call this method one after another
           foreach ($sub as $s) $main = merge_all($main, $s);
           return $main;
       }

       foreach ($sub as $key => $val) {
           # Main has same val as sub, next
           if ($main[$key] == $val) continue;
           # Main doesn't have the key, so set it
           if (!isset($main[$key])) {
               $main[$key] = $sub[$key];
               continue;
           }
           # Merge all sub-arrays as well
           if (is_array($val) && is_array($main[$key])) $main[$key] = merge_all($main[$key], $val);
       }

       return $main;
   }

The post-type IS getting registered and works in that sense, it's just not appearing in the admin-menu or in the admin-bar. It DOES show up by using a query and on menus as well.

I hope someone can help out, since I can't find the bug since over a month now -_- Thanks in advance

2

1 Answer 1

1

Your capability type is not valid. I would remove the code below if you can. WordPress auto generates the capability_type.

        "capability_type" => array(
            strtolower($s),
            strtolower($m)
        ),

Check out the documentation for capability_type. https://codex.wordpress.org/Function_Reference/register_post_type

1
  • 1
    Thanks allot, worked out :O Not sure why tho. This is quite weird since it shouldn't break it that bad because I did it like it said on the reference. Anyways, thanks allot again :)
    – PreFiXAUT
    Commented Jun 20, 2016 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.