{"id":22059158,"url":"https://github.com/93digital/custom-post-type","last_synced_at":"2026-05-07T08:32:45.926Z","repository":{"id":56938718,"uuid":"122485407","full_name":"93digital/custom-post-type","owner":"93digital","description":"Abandoned. This repository is no longer supported.","archived":false,"fork":false,"pushed_at":"2023-07-04T08:59:29.000Z","size":28,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-03T07:35:13.799Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/93digital.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-22T13:55:46.000Z","updated_at":"2023-07-04T08:59:34.000Z","dependencies_parsed_at":"2024-11-30T17:38:03.246Z","dependency_job_id":null,"html_url":"https://github.com/93digital/custom-post-type","commit_stats":{"total_commits":16,"total_committers":5,"mean_commits":3.2,"dds":0.6875,"last_synced_commit":"d37b3693f215e43add21587e575c306854c38867"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/93digital%2Fcustom-post-type","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/93digital%2Fcustom-post-type/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/93digital%2Fcustom-post-type/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/93digital%2Fcustom-post-type/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/93digital","download_url":"https://codeload.github.com/93digital/custom-post-type/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245136405,"owners_count":20566588,"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":[],"created_at":"2024-11-30T17:27:20.475Z","updated_at":"2026-05-07T08:32:45.898Z","avatar_url":"https://github.com/93digital.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Custom Post type utility functions.\n\nA PHP Class for creating WordPress Custom Post Types easily\n\n## Installation\n\n#### Install with composer\n\nRun the following in your terminal to install PostTypes with [Composer](https://getcomposer.org/).\n\n```\n$ composer require \"93digital/custom-post-type @dev\"\n```\n\nBelow is a basic example of getting started with the class, though your setup maybe different depending on how you are using composer.\n\n```\n\u003c?php\nrequire __DIR__ . '/vendor/autoload.php';\n\nuse Nine3\\PostType;\n\n$books = new PostType( 'book' );\n```\n\n#### Install Manually\n\nDownload the project and load the main class file into your themes functions.php like so:\n\n```\nrequire_once 'custom-post-type/class-nine3-custom-post-type.php';\n```\n\n## Post Types\n\n```\nfunction __construct( $singular, $plural = null, $icon = 'dashicons-format-aside', $supports = array(), $options = array() )\n```\n\n### Parameters\n\n- *$singular* (string|required) singular name.\n- *$plural* (string) plural name, by default will append 's' at the singular one.\n- $icon (string) the dashicon name or custom url.\n- $supports (array) the \"supports\" parameter. Default: title, editor and thumbnail.\n- $options (array) array of agruments to pass to register_post_type (See the [WordPress](https://codex.wordpress.org/Function_Reference/register_post_type#args) codex for all available options)\n\n### Create a new Post Type\n\nA new post type can be created by simply passing the post types name to the class constructor.\n\n```\n$books = new PostType( 'Book' );\n```\n\n### Defining plural and icon\n\n```\n$books = new PostType(\n  'Book',\n  'Books',\n  'dashicons-admin-page'\n);\n```\n\n[All icons list](https://developer.wordpress.org/resource/dashicons/)\n\n### Set the 'supports' parameter\n\nBy default the new custom post type set the following parameters:\n\n- title\n- editor\n- thumbnail\n\nTo override / change pass an array as 4th parameter, like:\n\n```\n$books = new PostType(\n  'Book',\n  'Books',\n  'dashicons-admin-page',\n  array(\n    'title',\n  )\n);\n```\n\n### Adding options\n\nYou can pass all the extra available parameters supported by the *register post type* function\n\n```\n$books = new PostType(\n  'Book',\n  'Books',\n  'dashicons-admin-page',\n  array(\n    'title',\n  ),\n  array(\n    'public' =\u003e false,\n  )\n);\n```\n\nAll available options are on the [WordPress Codex](https://codex.wordpress.org/Function_Reference/register_post_type)\n\n## Add Taxonomies\n\nAdding taxonomies to a post type is easily achieved by using the taxonomy() method.\n\n### Create new taxonomy\n\nTo create a new taxonomy simply pass the taxonomy name to the taxonomy() method. Labels and the taxonomy slug are generated from the taxonomy name.\n\n```\nfunction $books-\u003etaxonomy( string $singular_name, string $plural, array args );\n```\n\n#### Parameters:\n\n- *$singular* (string|required) singular name.\n- *$plural* (string) plural name, by default will append 's' at the singular one.\n- *args* (array) array of [Arguments](https://codex.wordpress.org/Function_Reference/register_taxonomy#Arguments).\n\n\n#### Example\n```\n$books-\u003etaxonomy( 'Genre' );\n```\n\n### Defining names\n\nYou can define the plural name by passing it as secondary parameter.\n\n- singular is the singular label for the post type\n- plural is the plural label for the post type\n- slug is the post type slug used in the permalinks\n\n```\n$books-\u003etaxonomy( 'Genre', 'Genres' );\n```\n\n### Adding options\n\nYou can further customise taxonomies by passing an array of options as the 3rd argument to the method.\n\n```\n$options = array(\n    'heirarchical' =\u003e false\n);\n\n$books-\u003etaxonomy( 'Genre', 'Genres', $options );\n```\n\nAll available options are on the [WordPress Codex](https://codex.wordpress.org/Function_Reference/register_taxonomy)\n\n## The settings page\n\nWhen setting the 'has_archive' parameter to true, the class will add a sub page for the new CPT, called:\n\n_[CPT Plura] Settings_\n\nThe page can be used to add fields using ACF, and will be visible inside the *Option page* option.\n\nTo retrieve the information stored inside the custom page, use:\n\n```\nget_field( '[THE OPTION SLUG]', 'cpt-[SINGULAR NAME]' );\nthe_field( '[THE OPTION SLUG]', 'cpt-[SINGULAR NAME]' );\n```\n\n_the 2nd attribute is the same of the \"page\" one of the setting page url_\n\n### Example\n\n```\nget_field( 'year', 'cpt-book' );\nthe_field( 'year', 'cpt-book' );\n```\n\n*By default the 'has_archive' parameter is set to true.*\n\n## Loop the items\n\nThe class has builtin methods to easily loop the custom posts, without manually calling the WP_Query class.\n\n\u003e **Note:** For performance purpose \"no_found_rows\" is set to true. \n\u003e If pagination is required, you need to set it to true.\n\n### Simple loop\n\nTo simple loop through the items using the default WP_Query parameters just use the variable registered before as:\n\n```\n\u003ch1\u003eThis is a simple loop\u003c/h1\u003e\n\u003c?php while ( $books-\u003ehave_posts() ) : $books-\u003ethe_post(); ?\u003e\n  \u003ch2\u003e\u003c?php the_title(); ?\u003e\u003c/h2\u003e\n  \u003cp\u003e\u003c?php the_content(); ?\u003e\u003c/p\u003e\n\u003c?php endwhile; ?\u003e\n```\n\nIn this example **$books** is the name of the variable used to register the custom post type in WP.\n\n### Get posts\n\nThe following functions allows to customise the WP_Query arguments used for the [Simple loop](#simple-loop).\n\n#### Get the CPT posts using custom parameters.\n\n```\nfunction get_posts( $posts_per_page = 0, $args );\n```\n\n- *$posts_per_page* (int) number of post to show per page. Default = 0 (does not set the parameter for WP_Query)\n- *$args* (array) array of arguments to pass to [WP_Query](https://codex.wordpress.org/Class_Reference/WP_Query)\n\n##### Example\n\n```\n// Get the first 10 books.\n$books-\u003eget_posts( 10 );\n\n// Passing offset attribute.\n$books-\u003eget_posts( 10, array( 'offset' =\u003e 11 ) );\n\n// If don't want to specify the post limit, just pass 0 as first parameter.\n$books-\u003eget_posts( 0, array( 'offset' =\u003e 11 ) );\n```\n\nUsing with simple loop:\n\n```\n$books-\u003eget_posts( 0, array( 'offset' =\u003e 11 ) );\n\nwhile ( $books-\u003ehave_posts() ) : $books-\u003ethe_post();\n  ...\nendwhile;\n\n```\n\nIf pagination is required\n\n```\n$books-\u003eget_posts( 0, array( 'offset' =\u003e 11, 'no_found_rows' =\u003e true ) );\n\nwhile ( $books-\u003ehave_posts() ) : $books-\u003ethe_post();\n  ...\nendwhile;\n```\n\n#### Get posts by meta values\n\n```\n$books-\u003eget_posts_by_meta( string $meta_key, mixed $meta_value, string $meta_compare, int $post_limit = 0 );\n```\n\n#### Get posts by taxonomy\n\nFor all the taxonomies registered using the built in [Create new taxonomy](#create-new-taxonomy) method, is available the following virutal method: \n\n```\n$books-\u003eget_posts_by_[taxonomy-slug]( string|array values );\n```\n\nwhere:\n\n- *$books* is the variable name previously used to register the custom post type.\n- *taxonomy_slug* is the slug of the taxonomy we want to filter.\n- $values list of slugs to filter.\n\n##### Example\n\nSuppose we register the taxonomy **genre** for our cpt:\n\n```\n$books = new PostType( 'Book' );\n$books-\u003etaxonomy( 'Genre' );\n```\n\nnow we can easily filter our posts:\n\n```\n$book_cpt-\u003eget_posts_by_genre( 'horror' );\n```\n\n**we can also pass an array of slugs if needed to filter by multiple values**\n\n##### Single taxonomy\n\nAn alternative method to filter by single taxonomy is:\n```\n$books-\u003eget_posts_by_term( string $taxonomy, string|int|array $slugs, string $field = 'slug', array $args = array() );\n```\n\n###### Parameters:\n\n- *$taxonomy* the taxonomy name\n- *$slugs* the slugs/ids to filter\n- *$field* Select taxonomy term by. Possible values are 'term_id', 'name', 'slug' or 'term_taxonomy_id'. Default value is 'slug'.\n- *$args* additional arguments to pass to the WP_Query class.\n\n##### Multiple taxonomies\n\n```\n$books-\u003eget_posts_by_terms( array $terms, $string relation = 'AND', array $args = array() );\n```\n\n###### Parameters:\n\n- *$terms* associative array with the taxonomies to filter.\n- *$relation* The logical relationship between each inner taxonomy.\n- *$args* additional arguments to pass to the WP_Query class.\n\n###### Example:\n\n```\n$books = new PostType( 'Book' );\n$books-\u003etaxonomy( 'Genre' );\n$books-\u003etaxonomy( 'Language' );\n\n/**\n * Retrieves all the \"Horror\" books written in \"Italian\" and \"German\"\n */\n$terms = array(\n  'genre' =\u003e 'horror',\n  'language' =\u003e array( 'italian', 'german' ),\n);\n$books = $books-\u003eget_posts_by_terms( $terms );\n```\n\n#### Set the order\n\nBy default posts are ordered by *Title*: *ASC*\n\n```\n$books-\u003eset_order( ORDER_BY, ORDER );\n```\n\n### The WP_Query object\n\nIs possible to access to the last executed WP_Query object with:\n\n```\n$query = $books-\u003ewp_query();\n```\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```\n$books-\u003efilters( array( 'genre' ) );\n```\n\n### Meta box\n\nThe class has 2 utility functions to easily add meta box for the CPT registered.\n\n#### Add meta box\n\n```\nfunction add_meta_box( $title, $callback, $context = 'normal', $priority = 'low' );\n```\n\n##### Example\n\n```\n$books-\u003eadd_meta_box( 'My meta box', 'my_callback_function' );\n\nfunction my_callback_function( $post ) {\n}\n```\n\n#### Sidebar meta box\n\nAn alternative method, instead of setting $contex = 'side', to register a meta box into the sidebar is:\n\n```\nfunction add_sidebar_meta_box( $title, $callback, $priority = 'low' );\n```\n\n##### Example\n\n```\n$books-\u003eadd_sidebar_meta_box( 'My meta box', 'my_callback_function' );\n\nfunction my_callback_function( $post ) {\n}\n```\n\n\n### Columns\n\n#### Add a custom column to the CPT list:\n\n```\nfunction add( $column, $label = null, $callback = null, $position = null );\n```\n\n##### Example\n\n```\n$books-\u003ecolumns()-\u003eadd( 'genre', 'Genre', 'genre_callback', 2 );\n\nfunction genre_callback( $key, $post_id ) {\n   echo $post_id;\n}\n```\n\n#### Hide a column from the CPT list\n\n```\n/**\n  * Add a column to hide\n  *\n  * @param  string $column the slug of the column to hdie\n  */\nfunction hide( $columns )\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F93digital%2Fcustom-post-type","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F93digital%2Fcustom-post-type","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F93digital%2Fcustom-post-type/lists"}