Custom Post Type API

The following is the list of available tools provided by the App\Model\AppCustomPostType class through Strata\Model\CustomPostType\CustomPostType. More in-depth information can be obtained on it’s detailed API page.

Public attributes

$this->admin_menus

Configures the model for automatic recognition of sub menus in the custom post type’s administration menu. See Automated admin menus for additional information.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
namespace App\Model;

class Career extends AppCustomPostType
{
    public $admin_menus = array(
        "viewEntries" => array(
            "title" => "Job applications",
            "menu-title" => "Job applications",
            "route" => array("Admin\CareerApplicationController", "viewEntries")
        )
    );
}
?>

$this->belongs_to

Configures custom post type Taxonomies by associating the current class with predefined taxonomy classes. See Custom Post Type Taxonomies for additional information.

1
2
3
4
5
6
7
8
9
10
11
<?php
namespace App\Model;

class Product extends AppCustomPostType
{
    public $belongs_to = array(
        "App\Model\Taxonomy\ProductLine",
        "App\Model\Taxonomy\ProductCategory"
    );
}
?>

$this->routed

Configures routes being generated by the Custom Post Type. See Resource Routing for additional information.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
namespace App\Model;

class ContactApplication extends AppCustomPostType
{
    public $routed = array(
        "rewrite" =>  array(
            'send_contact' => 'send-contact',
        ),
        'i18n' => array(
            'fr_CA' => array(
                "rewrite" => array(
                    'send_contact' => 'envoyer-la-demande',
                ),
            ),
            'es_ES' => array(
                "rewrite" => array(
                    'send_contact' => 'enviar-solicitud',
                ),
            ),
        ),
    );
}

?>

Public methods

$this->getLabel()

Returns a Strata\Model\CustomPostType\LabelParser object that exposes singular and plural labels automatically generated should none have been explicitly set during the custom post type registration.

1
2
3
4
5
6
7
8
9
<?php
    $item = new App\Model\Item();
    $labels = $item->getLabel();

    debug([
        $labels->plural(),
        $labels->singular(),
    ]);
?>

$this->getIcon()

Returns the model’s menu icon as specified by the menu_icon configuration key.

$this->hasTaxonomies()

Returns whether or not the current model supports and has taxonomies.

$this->getTaxonomies()

Gets the associated Taxonomy Strata objects.

self::wordpressKey()

Returns the internal custom post type slug from static access.

1
2
3
<?php
    App\Model\Item::wordpressKey() === "cpt_item;
?>

$this->getWordpressKey()

Returns the internal custom post type slug from an object reference.

1
2
3
4
<?php
    $item = new App\Model\Item();
    $item->getWordpressKey() === "cpt_item";
?>

self::factoryFromKey($wordpressKey)

Returns a Model reference from a likely $wordpressKey.

1
2
3
4
<?php
    $item = App\Model\AppModel::factoryFromKey("cpt_item");
    $item->getWordpressKey() === "cpt_item"
?>

self::getEntity()

Returns the Model’s corresponding ModelEntity object.

1
2
3
4
<?php
    $entity = App\Model\Item::getEntity(get_post());
    echo $entity->post_type;
?>

self::getEntityFromPost($post)

Returns a Model’s corresponding ModelEntity object based on a WP_Post object.

1
2
3
4
<?php
    $entity = App\Model\AppModel::getEntityFromPost(get_post());
    $entity->post_type === get_post_type;
?>

$this->getShortName()

Returns this object’s class name without the full namespace.

1
2
3
4
<?php
    $entity = new App\Model\Item();
    $entity->getShortName === "Item";
?>

$this->getConfig()

Fetches a value in the $configuration array in dot notation format.

1
2
3
4
<?php
    $entity = new App\Model\Item();
    debug($entity->getConfig("rewrite.slug"));
?>

$this->extractConfig()

Dynamically extracts a value in the $configuration array in dot notation format.

1
2
3
4
<?php
    $entity = new App\Model\Item();
    debug($entity->extractConfig("{s}.slug"));
?>

$this->setConfig($key, $value)

Saves a value in the $configuration array in dot notation format.

1
2
3
4
5
<?php
    $entity = new App\Model\Item();
    $entity->setConfig("is-custom-data", "yes!");
    $entity->getConfig("is-custom-data") === "yes!";
?>

$this->hasConfig($key)

Lookup the $configuration array and confirms the presence of a value using the dot notation format.

1
2
3
4
5
<?php
    $entity = new App\Model\Item();
    $entity->setConfig("is-custom-data", true);
    $entity->hasConfig("is-custom-data") === true;
?>

$this->getConfiguration()

Returns the model’s current $configuration array.

1
2
3
4
<?php
    $entity = new App\Model\Item();
    debug($entity->getConfiguration());
?>

Query methods

For additional information on each of these methods, have a look at the Custom Post Type Queries page.

static::repo()

Begins a database query against the current model. See Model Queries or Taxonomy Queries for more information.

1
2
3
4
5
<?php
    $firstDealerEntity = App\Model\Dealer::repo()
        ->published()
        ->first();
?>

$this->getQueryAdapter()

Allows you to customize the Query adapter on your model should you want to use your own custom object. The method must return an object inheriting Strata\Model\CustomPostType\Query.

$this->resetCurrentQuery()

Resets and forgets the current query state.

$this->query()

Returns the current, wrapped, wp_query object. Can be chained.

$this->date(array $dateQuery)

Applies a date query to the querier. Follow WP_Query’s documentation to learn how to format the parameters. Can be chained.

$this->orderby(string $orderBy)

Defines fields on which to order the current query. Can be chained.

$this->direction(string $dir)

Defines the direction on which the orderby() field will be sorted. Can be chained.

$this->status(string $status)

Shorthand for setting conditions on the post_status column. Can be chained.

$this->where(string $field, mixed $value)

The default catch all wrapper for WP_Query parameters. Can be chained.

$this->orWhere(string $field, mixed $value)

Allows the branching of different, still relatively simple, conditions in the same Wordpress query. Can be chained.

$this->limit(integer $qty)

Sets the maximum number of results the current query may return. Can be chained.

$this->offset(integer $offset)

Sets the starting offset of the current queries result set. Can be chained.

$this->byAuthorID(integer $offset)

Adds a ‘author’ condition to the current query. Can be chained.

$this->count()

Fetches the number of results the current queries returned.

This ends the current query.

$this->fetch()

Executes the query and resets it anew.

$this->listing($key, $value)

Executes the query and resets it anew.

This is useful for returning a list of matching elements in array($key => $value) format.

1
2
3
4
5
6
7
8
9
10
11
<?php
    $entities = App\Model\MyModel::repo()
        ->published()
        ->limit(2)
        ->listing("ID", "post_title");

    $entities === array(
        1234 => "Post Title 1",
        2345 => "Post Title 2",
    );
?>

$this->first()

Fetches the first element matching the current query and resets it anew.

$this->findAll()

Fetches the every matching elements the current query and resets it anew.

$this->findById($id)

Fetches a model entity with an ID of $id

$this->countTotal()

Returns the total number of entities of the current type.

Unlike count() this does not take additional filtering and maps to wp_count_posts() directly.

$this->paginate()

Paginates the current query. Must be called before the result set has been generated.

Maps to paginate_links().