PHPackages                             93digital/custom-post-type - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. 93digital/custom-post-type

AbandonedLibrary[Utility &amp; Helpers](/categories/utility)

93digital/custom-post-type
==========================

Custom Post type utility functions by 93digital

1673PHP

Since Jul 29Pushed 2y agoCompare

[ Source](https://github.com/93digital/custom-post-type)[ Packagist](https://packagist.org/packages/93digital/custom-post-type)[ RSS](/packages/93digital-custom-post-type/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Custom Post type utility functions.
===================================

[](#custom-post-type-utility-functions)

A PHP Class for creating WordPress Custom Post Types easily

Installation
------------

[](#installation)

#### Install with composer

[](#install-with-composer)

Run the following in your terminal to install PostTypes with [Composer](https://getcomposer.org/).

```
$ composer require "93digital/custom-post-type @dev"

```

Below is a basic example of getting started with the class, though your setup maybe different depending on how you are using composer.

```

```

In this example **$books** is the name of the variable used to register the custom post type in WP.

### Get posts

[](#get-posts)

The following functions allows to customise the WP\_Query arguments used for the [Simple loop](#simple-loop).

#### Get the CPT posts using custom parameters.

[](#get-the-cpt-posts-using-custom-parameters)

```
function get_posts( $posts_per_page = 0, $args );

```

- *$posts\_per\_page* (int) number of post to show per page. Default = 0 (does not set the parameter for WP\_Query)
- *$args* (array) array of arguments to pass to [WP\_Query](https://codex.wordpress.org/Class_Reference/WP_Query)

##### Example

[](#example-2)

```
// Get the first 10 books.
$books->get_posts( 10 );

// Passing offset attribute.
$books->get_posts( 10, array( 'offset' => 11 ) );

// If don't want to specify the post limit, just pass 0 as first parameter.
$books->get_posts( 0, array( 'offset' => 11 ) );

```

Using with simple loop:

```
$books->get_posts( 0, array( 'offset' => 11 ) );

while ( $books->have_posts() ) : $books->the_post();
  ...
endwhile;

```

If pagination is required

```
$books->get_posts( 0, array( 'offset' => 11, 'no_found_rows' => true ) );

while ( $books->have_posts() ) : $books->the_post();
  ...
endwhile;

```

#### Get posts by meta values

[](#get-posts-by-meta-values)

```
$books->get_posts_by_meta( string $meta_key, mixed $meta_value, string $meta_compare, int $post_limit = 0 );

```

#### Get posts by taxonomy

[](#get-posts-by-taxonomy)

For all the taxonomies registered using the built in [Create new taxonomy](#create-new-taxonomy) method, is available the following virutal method:

```
$books->get_posts_by_[taxonomy-slug]( string|array values );

```

where:

- *$books* is the variable name previously used to register the custom post type.
- *taxonomy\_slug* is the slug of the taxonomy we want to filter.
- $values list of slugs to filter.

##### Example

[](#example-3)

Suppose we register the taxonomy **genre** for our cpt:

```
$books = new PostType( 'Book' );
$books->taxonomy( 'Genre' );

```

now we can easily filter our posts:

```
$book_cpt->get_posts_by_genre( 'horror' );

```

**we can also pass an array of slugs if needed to filter by multiple values**

##### Single taxonomy

[](#single-taxonomy)

An alternative method to filter by single taxonomy is:

```
$books->get_posts_by_term( string $taxonomy, string|int|array $slugs, string $field = 'slug', array $args = array() );

```

###### Parameters:

[](#parameters-2)

- *$taxonomy* the taxonomy name
- *$slugs* the slugs/ids to filter
- *$field* Select taxonomy term by. Possible values are 'term\_id', 'name', 'slug' or 'term\_taxonomy\_id'. Default value is 'slug'.
- *$args* additional arguments to pass to the WP\_Query class.

##### Multiple taxonomies

[](#multiple-taxonomies)

```
$books->get_posts_by_terms( array $terms, $string relation = 'AND', array $args = array() );

```

###### Parameters:

[](#parameters-3)

- *$terms* associative array with the taxonomies to filter.
- *$relation* The logical relationship between each inner taxonomy.
- *$args* additional arguments to pass to the WP\_Query class.

###### Example:

[](#example-4)

```
$books = new PostType( 'Book' );
$books->taxonomy( 'Genre' );
$books->taxonomy( 'Language' );

/**
 * Retrieves all the "Horror" books written in "Italian" and "German"
 */
$terms = array(
  'genre' => 'horror',
  'language' => array( 'italian', 'german' ),
);
$books = $books->get_posts_by_terms( $terms );

```

#### Set the order

[](#set-the-order)

By default posts are ordered by *Title*: *ASC*

```
$books->set_order( ORDER_BY, ORDER );

```

### The WP\_Query object

[](#the-wp_query-object)

Is possible to access to the last executed WP\_Query object with:

```
$query = $books->wp_query();

```

Admin Edit Screen
-----------------

[](#admin-edit-screen)

### Filters

[](#filters)

When you register a taxonomy it is automagically added to the admin edit screen as a filter and a column.

You can define what filters you want to appear by using the filters() method:

```
$books->filters( array( 'genre' ) );

```

### Meta box

[](#meta-box)

The class has 2 utility functions to easily add meta box for the CPT registered.

#### Add meta box

[](#add-meta-box)

```
function add_meta_box( $title, $callback, $context = 'normal', $priority = 'low' );

```

##### Example

[](#example-5)

```
$books->add_meta_box( 'My meta box', 'my_callback_function' );

function my_callback_function( $post ) {
}

```

#### Sidebar meta box

[](#sidebar-meta-box)

An alternative method, instead of setting $contex = 'side', to register a meta box into the sidebar is:

```
function add_sidebar_meta_box( $title, $callback, $priority = 'low' );

```

##### Example

[](#example-6)

```
$books->add_sidebar_meta_box( 'My meta box', 'my_callback_function' );

function my_callback_function( $post ) {
}

```

### Columns

[](#columns)

#### Add a custom column to the CPT list:

[](#add-a-custom-column-to-the-cpt-list)

```
function add( $column, $label = null, $callback = null, $position = null );

```

##### Example

[](#example-7)

```
$books->columns()->add( 'genre', 'Genre', 'genre_callback', 2 );

function genre_callback( $key, $post_id ) {
   echo $post_id;
}

```

#### Hide a column from the CPT list

[](#hide-a-column-from-the-cpt-list)

```
/**
  * Add a column to hide
  *
  * @param  string $column the slug of the column to hdie
  */
function hide( $columns )

```

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity29

Early-stage or recently created project

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/ababa5d3352827682639627726c67624e3ab3829354c2132fe7d2a86f1e3946d?d=identicon)[93digital](/maintainers/93digital)

---

Top Contributors

[![MattKnight93](https://avatars.githubusercontent.com/u/83399317?v=4)](https://github.com/MattKnight93 "MattKnight93 (3 commits)")[![VicLobins93](https://avatars.githubusercontent.com/u/58182607?v=4)](https://github.com/VicLobins93 "VicLobins93 (3 commits)")[![AhmadAlAsadi93](https://avatars.githubusercontent.com/u/69842778?v=4)](https://github.com/AhmadAlAsadi93 "AhmadAlAsadi93 (1 commits)")

### Embed Badge

![Health badge](/badges/93digital-custom-post-type/health.svg)

```
[![Health](https://phpackages.com/badges/93digital-custom-post-type/health.svg)](https://phpackages.com/packages/93digital-custom-post-type)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
