BaseModel - Page-Carbajal/WPExpress GitHub Wiki

##WPExpress/Model/BaseModel Class

BaseModel is the Base Class for your Custom Post Types.

BaseModel implements Query and gives you some useful methods to manage your posts

Methods

A little less conversation a little more action.

###setPublic

Set public makes the post type public or private. It receives a boolean parameter indicating whether or not the post type will be public.

$this->setPublic(true);

###getPostTypeLabels

Returns an array containing the labels used to setup the Custom Post Type

$labels = $this->getPostTypeLabels();
print_r($labels);
// Outputs 
// Array
// ( name => "Books",
//   singular_name => "Book"
// )

###setPostTypeLabels

To be developed. It sets the properties for the Custom Post Type labels

###setSupportedFeatures

Any Custom Post Type in WordPress can support Title, Editor and Featured Image. With this method you can turn on or off those features.

This method receives 3 boolean parameters. supportTitle, supportEditor, supportThumbnail.

// Support Title and Editor but exclude thumbnail
$this->setSupportedFeatures(true, true, false);
// Support title and Thumbnail, exclude editor
$this->setSupportedFeatures(true, false, true);

###getSupportedFeatures Returns an array indication which features are supported by the Custom Post Type

// Support title and Thumbnail, exclude editor
$this->setSupportedFeatures(true, false, true);
$supportedFeatures = $this->getSupportedFeatures();
print_r($supportedFeatures);

//Outputs 
// Array(
//   title, thumbnail )

###registerCustomPostType

This method has to be called on your constructor to register the Custom Post Type

###getPermalink

Self explanatory.

$book = new Book(20);
echo $book->getPermalink();
// Prints the post permalink

###getThumbnailURL

If your Custom Post Type has a featured image, this method will return the URL to that image.

$book = new Book(20);
echo $book->getThumbnailURL();
// Prints the URL to the resource

###getAll

Queries the DB for all elements of the Custom Post Type. Returns an array of same class elements.

$list = MyClass::getAll();

foreach($list as $book)
{
    echo $book->getPermalink();
}

###getByField

Search the DB for Custom Post Types with the specified meta field and value. Returns an array of same class elements.

$list = MyClass::getByField( 'price', 50);

foreach($list as $book)
{
    echo $book->getPermalink();
}

###getByTaxonomy

Search the DB for Custom Post Types related to the given taxonomy. Returns an array of same class elements.

$list = MyClass::getByTaxonomy( 'author', 'Jaime Sabines');

foreach($list as $book)
{
    echo $book->getPermalink();
}

##To Be Developed

###getLatest

A helper to get the posts ordered by Date ASC

###getOldest

A helper to get the posts ordered by Date DESC

###getSome

A helper for paginated queries

##setFieldValue

Set the value for the meta field

##addRelation

Add a taxonomy relation

##deleteRelation

Remove a taxonomy relation

##save

A single method to update or create a post

##delete

A method to delete the post from the DB

###loadCustomFields

Returns a list of all the fields of the Custom Post Type

###getField

Returns the field value of the given field name

###addCustomField

Creates a meta field for this Custom Post Type