{"id":21882690,"url":"https://github.com/jjgrainger/wp-custom-post-type-class","last_synced_at":"2025-05-16T11:04:37.373Z","repository":{"id":56999550,"uuid":"10190955","full_name":"jjgrainger/wp-custom-post-type-class","owner":"jjgrainger","description":"A PHP Class for creating Wordpress Custom Post Types easily","archived":false,"fork":false,"pushed_at":"2017-05-19T12:59:24.000Z","size":106,"stargazers_count":416,"open_issues_count":12,"forks_count":113,"subscribers_count":45,"default_branch":"master","last_synced_at":"2025-05-12T13:53:59.671Z","etag":null,"topics":["custom-post-types","wordpress"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jjgrainger.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-05-21T08:16:11.000Z","updated_at":"2025-04-17T09:36:35.000Z","dependencies_parsed_at":"2022-08-21T13:20:44.160Z","dependency_job_id":null,"html_url":"https://github.com/jjgrainger/wp-custom-post-type-class","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jjgrainger%2Fwp-custom-post-type-class","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jjgrainger%2Fwp-custom-post-type-class/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jjgrainger%2Fwp-custom-post-type-class/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jjgrainger%2Fwp-custom-post-type-class/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jjgrainger","download_url":"https://codeload.github.com/jjgrainger/wp-custom-post-type-class/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254518384,"owners_count":22084374,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["custom-post-types","wordpress"],"created_at":"2024-11-28T09:34:25.999Z","updated_at":"2025-05-16T11:04:37.352Z","avatar_url":"https://github.com/jjgrainger.png","language":"PHP","funding_links":[],"categories":["Resources"],"sub_categories":["Custom Post Type"],"readme":"**N.B** I've released an updated version of the project to a new repository, [PostTypes](https://github.com/jjgrainger/PostTypes).\n\n# WP Custom Post Type Class v1.4\n\n[![Latest Stable Version](https://poser.pugx.org/jjgrainger/wp-custom-post-type-class/v/stable)](https://packagist.org/packages/jjgrainger/wp-custom-post-type-class) [![Total Downloads](https://poser.pugx.org/jjgrainger/wp-custom-post-type-class/downloads)](https://packagist.org/packages/jjgrainger/wp-custom-post-type-class) [![License](https://poser.pugx.org/jjgrainger/wp-custom-post-type-class/license)](https://packagist.org/packages/jjgrainger/wp-custom-post-type-class)\n\n\u003e A single class to help you build more advanced custom post types quickly.\n\n## Installation\n\n#### Install with Composer\n\nAdd the package to your projects `composer.json` file. Visit [getcomposer.org](http://getcomposer.org/) more information.\n\n```json\n{\n    \"require\": {\n        \"jjgrainger/wp-custom-post-type-class\": \"dev-master\"\n    }\n}\n```\n\n#### Install Manually\n\nDownload and include the class file into your themes `functions.php` like so:\n\n```php\ninclude_once('CPT.php');\n```\n\nand your ready to roll!\n\n## Creating a new Custom Post type\n\nTo create the post type simply create a new object\n\n```php\n$books = new CPT('book');\n```\n\nThe first parameter is the post type name and is required. ideally the post type name is all lowercase and words separated with an underscore `_`.\n\nto be specific about other post types names you can pass an associative array:\n\n`post_type_name` - the name of post type (singular, lowercase, underscores)\n\n`singular` - the singular label of the post type (Book, Person)\n\n`plural` - the plural of the post type (Books, People)\n\n`slug` - the permalink slug for the post type (plural, lowercase, hyphens)\n\nyou pass these names through the first parameter as an array like so:\n\n```php\n$people = new CPT(array(\n\t'post_type_name' =\u003e 'person',\n\t'singular' =\u003e 'Person',\n\t'plural' =\u003e 'People',\n\t'slug' =\u003e 'people'\n));\n```\n\nThe optional second parameter is the arguments for the post_type.\nsee [WordPress codex](http://codex.wordpress.org/Function_Reference/register_post_type#Parameters) for available options.\n\nThe Class uses the WordPress defaults where possible.\n\nTo override the default options simply pass an array of options as the second parameter. Not all options have to be passed just the ones you want to add/override like so:\n\n```php\n$books = new CPT('book', array(\n\t'supports' =\u003e array('title', 'editor', 'thumbnail', 'comments')\n));\n```\n\nSee the [WordPress codex](http://codex.wordpress.org/Function_Reference/register_post_type#Parameters) for all available options.\n\n## Existing Post Types\n\nTo work with exisiting post types, simply pass the post type name into the class constructor\n\n```php\n$blog = new CPT('post');\n```\n\n## Adding Taxonomies\n\nYou can add taxonomies easily using the `register_taxonomy()` method like so:\n\n```php\n$books-\u003eregister_taxonomy('genres');\n```\n\nthis method accepts two arguments, names and options. The taxonomy name is required and can be string (the taxonomy name), or an array of names following same format as post types:\n\n```php\n$books-\u003eregister_taxonomy(array(\n\t'taxonomy_name' =\u003e 'genre',\n\t'singular' =\u003e 'Genre',\n\t'plural' =\u003e 'Genres',\n\t'slug' =\u003e 'genre'\n));\n```\n\nAgain options can be passed optionally as an array. see the [WordPress codex](http://codex.wordpress.org/Function_Reference/register_taxonomy#Parameters) for all possible options.\n\n### Existing Taxonomies\n\nYou can add exisiting taxonomies to the post type by passing the taxonomy name through the `register_taxonomy` method. You will only need to specify the options for the custom taxonomy **once**, when its first registered.\n\n## Admin Edit Screen\n\n### Filters\n\nWhen you register a taxonomy it is *automagically* added to the admin edit screen as a filter and a column.\n\nYou can define what filters you want to appear by using the `filters()` method:\n\n```php\n$books-\u003efilters(array('genre'));\n```\n\nBy passing an array of taxonomy names you can choose the filters that appear and the order they appear in. If you pass an empty array, no drop down filters will appear on the admin edit screen.\n\n### Columns\n\nThe Class has a number of methods to help you modify the admin columns.\nTaxonomies registered with this class are automagically added to the admin edit screen as columns.\n\nYou can add your own custom columns to include what ever value you want, for example with our books post type we will add custom fields for a price and rating.\n\nThis class doesn't have any methods for adding custom fields as [Advanced Custom Fields (ACF)](http://advancedcustomfields.com) is way more awesome than anything this class could do!\n\nYou can define what columns you want to appear on the admin edit screen with the `columns()` method by passing an array like so:\n\n```php\n$books-\u003ecolumns(array(\n\t'cb' =\u003e '\u003cinput type=\"checkbox\" /\u003e',\n\t'title' =\u003e __('Title'),\n\t'genre' =\u003e __('Genres'),\n\t'price' =\u003e __('Price'),\n\t'rating' =\u003e __('Rating'),\n\t'date' =\u003e __('Date')\n));\n```\n\nThe key defines the name of the column, the value is the label that appears for that column. The following column names are *automagically* populated by the class:\n\n- any taxonomy registered through the object\n- `cb` the checkbox for bulk editing\n- `title` the post title with the edit link\n- `author` the post author\n- `post_id` the posts id\n- `icon`  the posts thumbnail\n\n\n#### Populating Columns\n\nYou will need to create a function to populate a column that isn't *automagically* populated.\n\nYou do so with the `populate_column()` method like so:\n\n```php\n$books-\u003epopulate_column('column_name', function($column, $post) {\n\n\t// your code goes here…\n\n});\n```\n\nso we can populate our price column like so:\n\n```php\n$books-\u003epopulate_column('price', function($column, $post) {\n\n\techo \"£\" . get_field('price'); // ACF get_field() function\n\n});\n```\n\nThe method will pass two variables into the function:\n\n* `$column` - The column name (not the label)\n* `$post` - The current post object\n\nThese are passed to help you populate the column appropriately.\n\n#### Sorting Columns\n\nIf it makes sense that column should be sortable by ascending/descending you can define custom sortable columns like so:\n\n```php\n$books-\u003esortable(array(\n\t'column_name' =\u003e array('meta_key', true)\n));\n```\n\nThe `true/false` is used to define whether the meta value is a string or integer,\nreason being is that if numbers are ordered as a string, numbers such as:\n\n\t1, 3, 5, 11, 14, 21, 33\n\nWould be ordered as:\n\n\t1, 11, 14, 21, 3, 33, 5\n\nBy adding the option true value the values will be sorted as integers, if false or undefined, the class will sort columns as string.\n\nso for our books example you will use:\n\n```php\n$books-\u003esortable(array(\n\t'price' =\u003e array('price', true),\n\t'rating' =\u003e array('rating', true)\n));\n```\n\n### Menu Icons\n\n#### Dashicons\n\nWith WordPress 3.8 comes [dashicons](https://developer.wordpress.org/resource/dashicons/) an icon font you can use with your custom post types. To use simply pass the icon name through the `menu_icon()` method like so:\n\n```php\n$books-\u003emenu_icon(\"dashicons-book-alt\");\n```\n\nFor a full list of icons and the class names to use visit [https://developer.wordpress.org/resource/dashicons/](https://developer.wordpress.org/resource/dashicons/)\n\n### Translation\n\nThe class is setup for translation, but if you need to set your own textdomain to work with your theme or plugin use the `set_textdomain()` method:\n\n```php\n$books-\u003eset_textdomain('your-textdomain');\n```\n\n### Flush Rewrite Rules\n\nYou can programmatically recreate the sites rewrite rules with the `flush()` method.\nThis is an expensive operation and should be used with caution, see [codex](https://codex.wordpress.org/Function_Reference/flush_rewrite_rules) for more.\n\n```php\n$books-\u003eflush();\n```\n\n## Notes\n\n* The class has no methods for making custom fields for post types, use [Advanced Custom Fields](http://advancedcustomfields.com)\n* The books example used in the README.md can be found in the [books-post-type.php](examples/books-post-type.php)\n* Licensed under the [MIT License](https://github.com/jjgrainger/wp-custom-post-type-class/blob/master/LICENSE)\n* Maintained under the [Semantic Versioning Guide](http://semver.org)\n\n## Author\n\n**Joe Grainger**\n* [http://jjgrainger.co.uk](http://jjgrainger.co.uk)\n* [http://twitter.com/jjgrainger](http://twitter.com/jjgrainger)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjjgrainger%2Fwp-custom-post-type-class","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjjgrainger%2Fwp-custom-post-type-class","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjjgrainger%2Fwp-custom-post-type-class/lists"}