Custom Post Type Queries

Strata Models generate Strata\Model\CustomPostType\Query adapters that hold configuration data and can be chained or manipulated before triggering the query lookup.

Up to the moment when fetch() or any of the executing commands are called, you can manipulate the query parameters at no cost.

It offers some of the advantages of a full-fledged ORM without bastardizing Wordpress’s WP_Query.

The following example shows how to query published posts ordered by the menu order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
namespace App;

class Artist extends AppCustomPostType {

    public function findPublished()
    {
        return $this->published()->byMenuOrder()->fetch();
    }

    public function published()
    {
        return $this->status("published");
    }

    public function byMenuOrder()
    {
        return $this->orderby("menu_order");
    }
}

You can call this query from your controllers by accessing the model repository using repo() :

1
2
3
<?php
    debug(Artist::repo()->findPublished());
?>

$this->where($field, $value);

Querying a Custom Post Type using where($field, $value) will eventually bubble up to Wordpress WP_Query object.

Each $field is added to an internal array that is then sent as first parameter of WP_Query when a call to a fetch method ends the chain.

Each of the allowed values that can be sent as $field are the same as those documented in Wordpress codex.

Conventions

There are conventions when building your model’s queries. Following them will make your code easier to understand.

  • Functions prefixed with find will execute a database lookup
  • All other querying functions must be chainable
  • Functions are not expected to be static as to not break inheritance
  • Queries made within Strata are expected to return ModelEntities
  • ModelEntities are expected to work transparently as a WP_Post or whichever default Wordpress object class.