{"id":14175,"date":"2011-08-17T21:33:08","date_gmt":"2011-08-17T21:33:08","guid":{"rendered":"https:\/\/wordpress.org\/plugins-wp\/posttypebuilder\/"},"modified":"2012-01-23T02:14:00","modified_gmt":"2012-01-23T02:14:00","slug":"posttypebuilder","status":"closed","type":"plugin","link":"https:\/\/wordpress.org\/plugins\/posttypebuilder\/","author":253893,"comment_status":"closed","ping_status":"closed","template":"","meta":{"version":"0.5","stable_tag":"0.5","tested":"3.3.2","requires":"3.0","requires_php":"","requires_plugins":"","header_name":"PostTypeBuilder","header_author":"Bj\u00f6rn Ali G\u00f6ransson","header_description":"","assets_banners_color":"","last_updated":"2012-01-23 02:14:00","external_support_url":"","external_repository_url":"","donate_link":"","header_plugin_uri":"","header_author_uri":"http:\/\/www.bedr.se\/","rating":0,"author_block_rating":0,"active_installs":10,"downloads":1182,"num_ratings":0,"support_threads":0,"support_threads_resolved":0,"author_block_count":0,"sections":["description"],"tags":[],"upgrade_notice":[],"ratings":{"1":0,"2":0,"3":0,"4":0,"5":0},"assets_icons":[],"assets_banners":[],"assets_blueprints":{},"all_blocks":[],"tagged_versions":["0.1","0.2","0.3","0.4","0.5"],"block_files":[],"assets_screenshots":{"screenshot-1.png":{"filename":"screenshot-1.png","revision":"1566888","resolution":"1","location":"plugin"}},"screenshots":{"1":"Automatic form field generation of first and second example (in the description tab)"}},"plugin_section":[],"plugin_tags":[50482,1241,153,24388,50481],"plugin_category":[59],"plugin_contributors":[97724],"plugin_business_model":[],"class_list":["post-14175","plugin","type-plugin","status-closed","hentry","plugin_tags-bornemix","plugin_tags-class","plugin_tags-database","plugin_tags-orm","plugin_tags-posttypebuilder","plugin_category-utilities-and-tools","plugin_contributors-bornemix","plugin_committers-bornemix"],"banners":[],"icons":{"svg":false,"icon":"https:\/\/s.w.org\/plugins\/geopattern-icon\/posttypebuilder.svg","icon_2x":false,"generated":true},"screenshots":[{"src":"https:\/\/ps.w.org\/posttypebuilder\/trunk\/screenshot-1.png?rev=1566888","caption":"Automatic form field generation of first and second example (in the description tab)"}],"raw_content":"<!--section=description-->\n<p>PostTypeBuilder is an Object Relational Mapper connecting directly into the Wordpress engine, and provides handy scaffolding through the Wordpress GUI, as well as <em>data querying<\/em> similar to LINQ and ActiveRecord (Book::find()-&gt;where(...)).<\/p>\n\n<pre><code>class Book extends Entity{\n    \/** @Property *\/\n    public $number_of_pages;\n}\n\n\n\nwhile(have_posts()){\n    the_post(); $book = new Book($post);\n\n    echo \"&lt;dt&gt;\" . $book-&gt;post_title . \"&lt;\/dt&gt;\";\n    echo \"&lt;dd&gt;\" . $book-&gt;number_of_pages . \"&lt;\/dd&gt;\";\n}\n<\/code><\/pre>\n\n<p>Included is the <a href=\"http:\/\/code.google.com\/p\/addendum\/\">Addendum<\/a> library to support @annotations.<\/p>\n\n<p>This plugin saves no information on your system (no database tables, no temporary files). All information is supplied by you in your class files.<\/p>\n\n<p><em>See <a href=\"http:\/\/wordpress.org\/extend\/plugins\/posttypebuilder\/other_notes\/\">Other notes<\/a> for more info<\/em><\/p>\n\n<h4>Register classes<\/h4>\n\n<p>Any classes in <code>wp-content\/classes<\/code>, where the filename (like <code>class_name.php<\/code>) corresponds to the classname (like <code>ClassName<\/code>), will be registered as a Wordpress Custom Post Type.<\/p>\n\n<p>Following example needs to be defined in <code>wp-content\/classes\/book.php<\/code>, and results in a registered post type being shown in the admin UI (but not publicly queryable):<\/p>\n\n<pre><code>namespace MyEntities;\nuse \\PostTypeBuilder\\Entity;\n\nclass Book extends Entity{\n}\n<\/code><\/pre>\n\n<p><em>See <a href=\"http:\/\/wordpress.org\/extend\/plugins\/posttypebuilder\/other_notes\/\">Other notes<\/a> for more info<\/em><\/p>\n\n<h4>Define properties<\/h4>\n\n<p>Any class properties prefixed by the annotational comment <code>\/** @Property *\/<\/code> will be registered as Wordpress metadata, and form fields will appear in the edit\/create screen of the post type corresponding to the property type.<\/p>\n\n<p>Following example, building on the previous <code>Book<\/code> example class, will display a text form field in the edit\/create post screen:<\/p>\n\n<pre><code>namespace MyEntities;\nuse \\PostTypeBuilder\\Entity;\n\nclass Book extends Entity{\n    \/** @Property *\/\n    public $number_of_pages;\n}\n<\/code><\/pre>\n\n<p>To enable the property to carry multiple values, use the <code>= array()<\/code> assignment:<\/p>\n\n<pre><code>\/** @Property *\/\npublic $authors = array();\n<\/code><\/pre>\n\n<p><em>See <a href=\"http:\/\/wordpress.org\/extend\/plugins\/posttypebuilder\/other_notes\/\">Other notes<\/a> for more info<\/em><\/p>\n\n<h4>Find entities<\/h4>\n\n<p>You can load entities in three ways.<\/p>\n\n<p>(1) By post ID:<\/p>\n\n<pre><code>$book = new Book(21);\n<\/code><\/pre>\n\n<p>(2) By post object (useful in the loop):<\/p>\n\n<pre><code>global $post;\n\n$book = new Book($post);\n<\/code><\/pre>\n\n<p>(3) By query:<\/p>\n\n<pre><code>$books = Book::find()-&gt;where(\"pages &gt; (NUMERIC)\", 10);\n\nforeach($books as $book){ ... }\n<\/code><\/pre>\n\n<p>(Note that the query is executed lazily - ie. at the foreach statement)<\/p>\n\n<p>Following code shows how to manipulate your entities:<\/p>\n\n<pre><code>$book = new Book();\n$book-&gt;post_title = \"Foo\";\n$book-&gt;save();\n<\/code><\/pre>\n\n<p>(Both post_object members and class properties are accessed in a unified way, but class properties have precedence)<\/p>\n\n<p><em>See <a href=\"http:\/\/wordpress.org\/extend\/plugins\/posttypebuilder\/other_notes\/\">Other notes<\/a> for more info<\/em><\/p>\n\n<h4>Extend the functionality<\/h4>\n\n<p>The plugin is designed to be extensible, so you can override form field generation for your classes, text representations (by overriding __toString), add your own property types, their form field generation, their text representation, you can hook into save events (and more...).<\/p>\n\n<h3>Register classes<\/h3>\n\n<p>At Wordpress initialization stage, when an HTTP request is made, PostTypeBuilder searches <code>wp-content\/classes<\/code> for PHP files.<\/p>\n\n<p>Each file is then included, and PostTypeBuilder determines if class <code>\\MyEntities\\\u03b1<\/code> was defined as a result of that inclusion. <code>MyEntities<\/code> is the required namespace where you need to define your entities, and <code>\u03b1<\/code> is the filename converted to CamelCase.<\/p>\n\n<p>For example, if file <code>wp-content\/classes\/newsletter_issue.php<\/code> exists, PostTypeBuilder will include that and then check if the class <code>\\MyEntities\\NewsletterIssue<\/code> exists.<\/p>\n\n<p><em>To change the default namespace, define POSTTYPEBUILDER_ENTITIES_NAMESPACE before PostTypeBuilder gets ahold of it.<\/em><\/p>\n\n<h4>Options<\/h4>\n\n<p>There are a number of options available (defined in <code>annotations.php<\/code>):<\/p>\n\n<ul>\n<li><code>@CanonicalName(\"\")<\/code><\/li>\n<li><code>@Name(\"\")<\/code><\/li>\n<li><code>@PluralName(\"\")<\/code><\/li>\n<li><code>@Labels({})<\/code><\/li>\n<li><code>@Options({})<\/code><\/li>\n<li><code>@Panels({})<\/code><\/li>\n<\/ul>\n\n<p>To use these, include those you want in a single <code>\/**  *\/<\/code> annotational \"DocBlock\" comment, with each annotation separated by some whitespace (preferrably, each on a separate line).<\/p>\n\n<p><em><code>@CanonicalName(\"\")<\/code><\/em> defines that the wordpress-internal name should equal the string that is supplied between the parenthesis. By default, this will be equal to the PHP filename, excluding the <code>.php<\/code> extension. (ie. \"book\")<\/p>\n\n<p>The following example registers a Post class, with the internal, canonical name \"content_post\":<\/p>\n\n<pre><code>namespace MyEntities;\nuse \\PostTypeBuilder\\Entity;\n\n\/** @CanonicalName(\"content_post\") *\/\nclass Post extends Entity{\n}\n<\/code><\/pre>\n\n<p><em><code>@Name(\"\")<\/code><\/em> defines the singular name (used in labels, menus) to be the string supplied between the parenthesis. By default, this will be equal to the CamelCase version of the canonical name, each Camel segment being separated by space. (ie. \"Book\")<\/p>\n\n<p><em><code>@PluralName(\"\")<\/code><\/em> defines the plural name (used in labels, menus) to be the string supplied between the parenthesis. By default, this will be equal to the implicit (or explicit) singular name + \"s\". (ie. \"Books\")<\/p>\n\n<p>The following example defines the irregular plural name \"Virii\" for the class \"Virus\":<\/p>\n\n<pre><code>namespace MyEntities;\nuse \\PostTypeBuilder\\Entity;\n\n\/** @PluralName(\"Virii\") *\/\nclass Virus extends Entity{\n}\n<\/code><\/pre>\n\n<p><em><code>@Labels({})<\/code><\/em> is a way to override the PostTypeBuilder label determination mechanism, which is partly described above (see <code>generate_class_meta<\/code> in <code>posttypebuilder.php<\/code>). Supplied values will override the values provided by PostTypeBuilder. Possible keys correspond to the keys in the labels object documented in <a href=\"http:\/\/codex.wordpress.org\/Function_Reference\/register_post_type\">register_post_type()<\/a>.<\/p>\n\n<p>The following example overrides the menu name (subsequent label assignations need to separated by comma):<\/p>\n\n<pre><code>namespace MyEntities;\nuse \\PostTypeBuilder\\Entity;\n\n\/** @Labels({menu_name = \"Foo\"}) *\/\nclass Book extends Entity{\n}\n<\/code><\/pre>\n\n<p><em><code>@Options({})<\/code><\/em> is a way to override the option defaults that are passed to WP's <a href=\"http:\/\/codex.wordpress.org\/Function_Reference\/register_post_type\">register_post_type()<\/a>.<\/p>\n\n<p>By default, entity types are not enabled to be visible to outside users. As such, you will get a 404 error when clicking \"View post\".<\/p>\n\n<p>The following example enables \"publicly queryable\" for the entity type:<\/p>\n\n<pre><code>namespace MyEntities;\nuse \\PostTypeBuilder\\Entity;\n\n\/** @Options({publicly_queryable = true}) *\/\nclass Book extends Entity{\n}\n<\/code><\/pre>\n\n<p>The following example enables publicly queryable and post archives:<\/p>\n\n<pre><code>namespace MyEntities;\nuse \\PostTypeBuilder\\Entity;\n\n\/** @Options({\n    publicly_queryable = true,\n    has_archive = \"books\"\n}) *\/\nclass Book extends Entity{\n}\n<\/code><\/pre>\n\n<p>The following example enables support for text editor and comments:<\/p>\n\n<pre><code>namespace MyEntities;\nuse \\PostTypeBuilder\\Entity;\n\n\/** @Options({\n    supports = {\"title\",\"editor\",\"thumbnail\",\"comments\"}\n}) *\/\nclass Book extends Entity{\n}\n<\/code><\/pre>\n\n<p><em><code>@Panels<\/code><\/em> specifies the form field groups (\"panels\") that are to be shown on the edit\/create post screen. By default, only one panel is available (\"properties\"). Supplying @Panels will clear the default panel, so you need to redeclare it if you use this annotation.<\/p>\n\n<p>The following code specifies that two panels, \"Properties\" (in main column) and \"Options\" (in side column) should be used:<\/p>\n\n<pre><code>namespace MyEntities;\nuse \\PostTypeBuilder\\Entity;\n\n\/**\n@Panels({\n    {\n        id=\"properties\",\n        label=\"Properties\",\n        position=\"normal\"\n    },\n    {\n        id=\"options\",\n        label=\"Options\",\n        position=\"side\"\n    }\n})\n*\/\nclass Book extends Entity{\n    \/** @Property(panel=\"properties\") *\/\n    public $author_name;\n    \/** @Property(type=\"Boolean\",panel=\"options\") *\/\n    public $show_on_startpage;\n}\n<\/code><\/pre>\n\n<h3>Define properties<\/h3>\n\n<p>Each property that PostTypeBuilder is to be aware of, needs to be prefixed with the annotational comment <code>\/** @Property *\/<\/code>. Otherwise it will be a plain old PHP class property (not managed by PostTypeBuilder).<\/p>\n\n<p>By default, the property type is \"Text\", which can be declared explicitly by including <code>\/** @Property(type=\"Text\") *\/<\/code>.<\/p>\n\n<p>The following example defines a property called <code>number_of_pages<\/code> in the entity Book:<\/p>\n\n<pre><code>namespace MyEntities;\nuse \\PostTypeBuilder\\Entity;\n\nclass Book extends Entity{\n    \/** @Property *\/\n    public $number_of_pages;\n}\n<\/code><\/pre>\n\n<p><em>Note: The specific visibility <code>public<\/code> is not required - but without any modifier, <a href=\"http:\/\/php.net\/manual\/en\/language.oop5.visibility.php\">the variable name will not be registered as a class property<\/a>. Either of <code>public<\/code>, <code>private<\/code> or <code>protected<\/code> will do (<code>var<\/code> evaluates to <code>public<\/code>), but note that only public properties will show up in the edit\/create post screen.<\/em><\/p>\n\n<p>By prefixing the property declaration with the <code>@Property<\/code> annotation, PostTypeBuilder will:<\/p>\n\n<ol>\n<li>Sync it when relevant instances are loaded\/saved from the database.<\/li>\n<li>Generate a form field for it when showing the edit\/create post screen.<\/li>\n<li>Make it available for searching\/filtering\/ordering when using the PostTypeBuilder query mechanism.<\/li>\n<\/ol>\n\n<h4>Multiple values<\/h4>\n\n<p>A property can hold multiple values if declared with <code>= array()<\/code> in the class definition. Form field will then be preceded by a checkbox-list of current values that can be rearranged by drag and drop, and unchecked to be removed. Adding input to form field will add a new value.<\/p>\n\n<p>Since 0.4, PostTypeBuilder uses WP's post_meta functions which takes care of array serialization when saving.<\/p>\n\n<p>The following example enables the \"author_names\" property to hold multiple values:<\/p>\n\n<pre><code>namespace MyEntities;\nuse \\PostTypeBuilder\\Entity;\n\nclass Book extends Entity{\n    \/** @Property *\/\n    public $author_names = array();\n}\n<\/code><\/pre>\n\n<h4>Options<\/h4>\n\n<p>To supply options to this annotation, include them in a comma-separated list of equals-separated name-value pairs, the list surrounded by braces between the optional parenthesis after the annotation keyword: <code>\/** Property({option1=\"Foo\", option2=\"Bar\"}) *\/<\/code><\/p>\n\n<p>There are a few options you can supply to this annotation:<\/p>\n\n<ul>\n<li><code>type<\/code> (default <code>\"Text\"<\/code>)<\/li>\n<li><code>label<\/code> (default <code>null<\/code>)<\/li>\n<li><code>required<\/code> (default <code>false<\/code>)<\/li>\n<li><code>panel<\/code> (default <code>\"properties\"<\/code>)<\/li>\n<li><code>visible<\/code> (default <code>true<\/code>)<\/li>\n<\/ul>\n\n<p><em><code>type<\/code><\/em> specifies the class property type, either a built-in PostTypeBuilder type, an entity declared in MyEntities, or a custom type supplied by you (look in <code>types.php<\/code> for inspiration).<\/p>\n\n<p>PostTypeBuilder supplies a number of built-in types:<\/p>\n\n<ul>\n<li><code>Text<\/code> (default) - Long string.<\/li>\n<li><code>LongText<\/code> - Long string, but the form field will be a multiline <code>&lt;textarea&gt;<\/code> as opposed to <code>Text<\/code>.<\/li>\n<li><code>Boolean<\/code> - True\/false. Form field will be a checkbox. Saved value will be (string) <code>\"true\"<\/code> for true, or <code>null<\/code> for false.<\/li>\n<li><code>GenericPost<\/code> - Link to a post ID without specifying type. Form field will be a text input field. Saved value will be post ID.<\/li>\n<li><code>Image<\/code> - Link to an attachment image. Form field will be a rudimentary image chooser. Saved value will be attachment ID.<\/li>\n<li><code>User<\/code> - Link to a user in the Wordpress database. Form field will be a <code>&lt;select&gt;<\/code> dropdown box. Saved value will be user ID.<\/li>\n<li><code>Enum<\/code> - Long string. Form field will be a <code>&lt;select&gt;<\/code> dropdown box. Saved value will be the value part (not the label part) of the selected enum-options item.<\/li>\n<\/ul>\n\n<p>The following example defines a <code>Book<\/code> which contains a collection of <code>User<\/code>s who \"liked\" it:<\/p>\n\n<pre><code>namespace MyEntities;\nuse \\PostTypeBuilder\\Entity;\n\nclass Book extends Entity{\n    \/** @Property({type=\"User\"}) *\/\n    public $users_who_liked_this_book = array();\n}\n<\/code><\/pre>\n\n<p><em><code>Enum<\/code><\/em> is kind of special, because it requires another parameter to be specified (<code>enum_options<\/code>). It is a comma-separated list of equals-separated value-label pairs, enclosed by curly braces, like: <code>{value_one=\"Label 1\",value_two=\"Label 2\"}<\/code><\/p>\n\n<p>The following example defines a <code>Book<\/code> which contains a property specifying the difficulty of the books' language:<\/p>\n\n<pre><code>namespace MyEntities;\nuse \\PostTypeBuilder\\Entity;\n\nclass Book extends Entity{\n    \/** @Property({type=\"Enum\",enum_options={easy=\"Easy\",medium=\"Medium\",hard=\"Hard\"}}) *\/\n    public $users_who_liked_this_book = array();\n}\n<\/code><\/pre>\n\n<p><em>It is also possible to define an entity as your property type.<\/em> This part is kind of special.<\/p>\n\n<p>If you have an entity class called \"Book\", then you can make the property link to it by specifying \"Book\" as its type.<\/p>\n\n<p><em>You need to name your property with an ending \"_id\". For properties with multiple values, it must end with _ids.<\/em><\/p>\n\n<pre><code>\/** @Property(type=\"Book\") *\/\npublic $favorite_book_id;\n<\/code><\/pre>\n\n<p>Now, when accessing your entity object, you can get its associated object by accessing the property name <em>without id<\/em>:<\/p>\n\n<pre><code>$entity = ...\n\n$book = $entity-&gt;favorite_book;\n<\/code><\/pre>\n\n<p>The preceding code will give you the whole Entity object, loaded with properties and all. To get just the id, use the proper property name:<\/p>\n\n<pre><code>$entity = ...\n\n$book_id = $entity-&gt;favorite_book_id;\n<\/code><\/pre>\n\n<p>When having properties with multiple values, name your code with the singular name + \"_ids\":<\/p>\n\n<pre><code>\/** @Property(type=\"Book\") *\/\npublic $favorite_book_ids;\n<\/code><\/pre>\n\n<p>PostTypeBuilder listens for the property name minus \"_ids\" plus \"s\", like the following code:<\/p>\n\n<pre><code>$entity = ...\n\n$books = $entity-&gt;favorite_books;\n<\/code><\/pre>\n\n<p>To only get the IDs, use the following code:<\/p>\n\n<pre><code>$entity = ...\n\n$book_ids = $entity-&gt;favorite_book_ids;\n<\/code><\/pre>\n\n<p><em>Note: To change the collection, you need to manipulate the IDs array, not the lazily loaded proxy properties.<\/em><\/p>\n\n<p><em><code>label<\/code><\/em> specifies the text used for labelling the form field on the edit\/create post screen. Currently, PostTypeBuilder does not support i18n for any of its interface functionality.<\/p>\n\n<p>The following example specifies the label for two properties:<\/p>\n\n<pre><code>namespace MyEntities;\nuse \\PostTypeBuilder\\Entity;\n\nclass Book extends Entity{\n    \/** @Property(label=\"Number of pages (do not use this one!)\") *\/\n    public $pages;\n    \/** @Property(type=\"Image\", label=\"Image of first page\") *\/\n    public $first_page_image;\n}\n<\/code><\/pre>\n\n<p><em><code>required<\/code><\/em> specifies that the property must not be empty. Currently, the only thing happening when this option is set to <code>true<\/code>, a red star comes up next to the label. No validation of any kind is made.<\/p>\n\n<p><em><code>panel<\/code><\/em> specifies which panel should contain the form field on the edit\/create post screen. Look for the Panels section below.<\/p>\n\n<p><em><code>visible<\/code><\/em> specifies wether the form field should be shown on screen or not. If not, the only way to interact with the property is through code.<\/p>\n\n<h3>Find entities<\/h3>\n\n<p><em>This section is not done!<\/em><\/p>\n\n<p>To start using the query mechanism, you will need to retrieve a Query object. This is done like so:<\/p>\n\n<pre><code>$query_object = Book::find();\n<\/code><\/pre>\n\n<p>This object can then be used to add parameters to what will add up to an WP Query object, which will be executed when accessed.<\/p>\n\n<p>To limit the amount of hits in your query to 5, use the following code:<\/p>\n\n<pre><code>$query_object = Book::find(5);\n\n\n\n$first_item = $query_object[0]; \/\/ executes search and returns first match\n$second_item = $query_object[1]; \/\/ by now, result is already cached in $query_object, and another query will not be done\n\n\n\nforeach($query_object as $item){ \/\/ executes search and returns iterator\n    ...\n}\n<\/code><\/pre>\n\n<p>When having executed the search query, changes to the query object will have no effect.<\/p>\n\n<p>When making the query, the query acts as a proxy for its found instances. Even things like <code>$book_title = Book::find()-&gt;where(\"author_name\", \"Bj\u00f6rn Ali G\u00f6ransson\")-&gt;post_title<\/code> will work. To get the real entity object, use <code>get($index = 0)<\/code> or array notation <code>[$index]<\/code>.<\/p>\n\n<p>The following example does a search for book author name:<\/p>\n\n<pre><code>$query_object = Book::find();\n$query_object-&gt;where(\"author_name\", \"Bj\u00f6rn Ali G\u00f6ransson\");\n$book = $query_object[0];\n<\/code><\/pre>\n\n<p>Almost each query method returns the query object itself, by the way, which makes way for chaining:<\/p>\n\n<pre><code>$book = Book::find()-&gt;where(\"author_name\", \"Bj\u00f6rn Ali G\u00f6ransson\")-&gt;get(0);\n<\/code><\/pre>\n\n<h4>Where<\/h4>\n\n<p>The syntax is as following:<\/p>\n\n<pre><code>$query_object-&gt;where(key, value);\n<\/code><\/pre>\n\n<p>To make a search for a property, you use the same syntax. When searching for properties, though, you have more options.<\/p>\n\n<p>The following example searches for books where <code>year<\/code> is between 1950 and 2000:<\/p>\n\n<pre><code>$query_object = Book::find();\n\n$query_object-&gt;where(\"year &gt; (NUMERIC)\", 1950);\n\n$query_object-&gt;where(\"year &lt; (NUMERIC)\", 2000);\n<\/code><\/pre>\n\n<p>The first parameter can be either <code>name<\/code>, <code>name operator<\/code> or <code>name operator (format)<\/code>.<\/p>\n\n<pre><code>name should be a valid PostTypeBuilder property.\n<\/code><\/pre>\n\n<p>Operator should be one of the following:<\/p>\n\n<ul>\n<li><code>=<\/code><\/li>\n<li><code>!=<\/code><\/li>\n<li><code>&gt;<\/code><\/li>\n<li><code>&gt;=<\/code><\/li>\n<li><code>&lt;<\/code><\/li>\n<li><code>&lt;=<\/code><\/li>\n<li><code>LIKE<\/code><\/li>\n<li><code>NOT LIKE<\/code><\/li>\n<li><code>IN<\/code><\/li>\n<li><code>NOT IN<\/code><\/li>\n<li><code>BETWEEN<\/code><\/li>\n<li><code>NOT BETWEEN<\/code><\/li>\n<\/ul>\n\n<p><em>If you don't specify an operator, then like WP_Query, it will default to <code>=<\/code>.<\/em><\/p>\n\n<pre><code>format specifies to which data type the value should be casted.\n<\/code><\/pre>\n\n<p>Possible values are:<\/p>\n\n<ul>\n<li><code>NUMERIC<\/code><\/li>\n<li><code>BINARY<\/code><\/li>\n<li><code>CHAR<\/code><\/li>\n<li><code>DATE<\/code><\/li>\n<li><code>DATETIME<\/code><\/li>\n<li><code>DECIMAL<\/code><\/li>\n<li><code>SIGNED<\/code><\/li>\n<li><code>TIME<\/code><\/li>\n<li><code>UNSIGNED<\/code>.<\/li>\n<\/ul>\n\n<p><em>If you don't specify a format, then like WP_Query, it will default to <code>CHAR<\/code>.<\/em><\/p>\n\n<h3>Extend default functionality<\/h3>\n\n<h4>Custom form fields<\/h4>\n\n<p>To get custom form fields for a type, extend the type in your own file (maybe <code>functions.php<\/code> in your theme?) and implement your own <code>generate_input_field<\/code> method. See <code>types.php<\/code> for inspiration.<\/p>\n\n<p>In order to implement custom form fields for choosing an entity, override the <code>generate_input_field<\/code> in your entity class.<\/p>\n\n<p>The following example overrides the <code>&lt;select&gt;<\/code> dropdown form field for selecting Books, as it would be too long to load such a list, and instead delegates the <code>generate_input_field<\/code> to the <code>Text<\/code> type (and thereby exposing the ID):<\/p>\n\n<pre><code>namespace MyEntities;\nuse \\PostTypeBuilder\\Entity;\n\nclass Book extends Entity{\n    public function generate_input_field($name_and_id, $value = null, $property = null, $property_annotation = null, $many = false){\n        \\Addendum\\TextPropertyType::generate_input_field($name_and_id, $value, $property, $property_annotation, $many);\n    }\n}\n<\/code><\/pre>\n\n<p>See <code>entity.class.php<\/code> and <code>types.php<\/code> for inspiration on how to implement this method.<\/p>\n\n<h4>Text representation of entities<\/h4>\n\n<p>When listing entities for form fields, the text representation of the entity is used to represent it. It defaults to the post title, but if that doesn't exist, it shows other things (see <code>entity.class.php<\/code>). This functionality can be changed by overriding the <code>__toString()<\/code> method.<\/p>\n\n<p>The following example shows the authors in the books' text representation:<\/p>\n\n<pre><code>namespace MyEntities;\nuse \\PostTypeBuilder\\Entity;\n\nclass Book extends Entity{\n    \/** @Property *\/\n    public $author_names = array();\n\n    public function __toString(){\n        return $this-&gt;post_title . \" by \" . implode(\", \", $this-&gt;author_names);\n    }\n}\n<\/code><\/pre>\n\n<h4>Class methods<\/h4>\n\n<p>Class methods can be added without any \"plumbing\".<\/p>\n\n<p>The following example adds a \"sort\" method to the Book class:<\/p>\n\n<pre><code>namespace MyEntities;\nuse \\PostTypeBuilder\\Entity;\n\nclass Book extends Entity{\n    \/** @Property *\/\n    public $author_names;\n\n    public function sort_names(){\n        sort($this-&gt;author_names);\n    }\n}\n<\/code><\/pre>\n\n<p>The method can then be used like the following code:<\/p>\n\n<pre><code>$book = ...\n\n$book-&gt;sort();\n\n$book-&gt;save();\n<\/code><\/pre>\n\n<h4>Event hooks<\/h4>\n\n<p>You may \"hook on\" certain post events by defining corresponding class methods. They need to be static, as an entity instance isn't always desirable in these events. All methods receive the associated post object as argument.<\/p>\n\n<p>Following is a list of all methods that can be implemented:<\/p>\n\n<ul>\n<li><code>clean_post_cache<\/code> - Runs when post cache is cleaned.<\/li>\n<li><code>delete_post<\/code> - Runs when a post or page is about to be deleted.<\/li>\n<li><code>edit_post<\/code> - Runs when a post or page is updated\/edited, including when a comment is added or updated (which causes the comment count for the post to update).<\/li>\n<li><code>save_post<\/code> - Runs whenever a post or page is created or updated, which could be from an import, post\/page edit form, xmlrpc, or post by email.<\/li>\n<li><code>publish_post<\/code> - Runs when a post is published, or if it is edited and its status is \"published\".<\/li>\n<li><code>pending_post<\/code> - Same as <code>publish_post<\/code> but with status \"pending\".<\/li>\n<li><code>draft_post<\/code> - Same as <code>publish_post<\/code> but with status \"draft\".<\/li>\n<li><code>auto_post<\/code> - Same as <code>publish_post<\/code> but with status \"auto\".<\/li>\n<li><code>future_post<\/code> - Same as <code>publish_post<\/code> but with status \"future\".<\/li>\n<li><code>private_post<\/code> - Same as <code>publish_post<\/code> but with status \"private\".<\/li>\n<li><code>inherit_post<\/code> - Same as <code>publish_post<\/code> but with status \"inherit\".<\/li>\n<li><code>trash_post<\/code> - Same as <code>publish_post<\/code> but with status \"trash\".<\/li>\n<\/ul>\n\n<p>The following example sorts author names on save:<\/p>\n\n<pre><code>namespace MyEntities;\nuse \\PostTypeBuilder\\Entity;\n\nclass Book extends Entity{\n    \/** @Property *\/\n    public $author_names;\n\n    public function sort_names(){\n        sort($this-&gt;author_names);\n    }\n\n    static function save_post($post_object){\n        $book = new Book($post_object);\n\n        $book-&gt;sort_names();\n\n        $book-&gt;save();\n    }\n}\n<\/code><\/pre>\n\n<p><em>Note: The save_post will not fire recursively into itself, which means that the last call to <code>$book-&gt;save()<\/code> will not make the <code>save_post()<\/code> execute again.<\/em><\/p>","raw_excerpt":"Maps @annotated PHP classes to custom post types, automatically adds relevant fields to the Admin GUI, and LINQ\/ActiveRecord-like queries.","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin\/14175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin"}],"about":[{"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/types\/plugin"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/comments?post=14175"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wporg\/v1\/users\/bornemix"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/media?parent=14175"}],"wp:term":[{"taxonomy":"plugin_section","embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_section?post=14175"},{"taxonomy":"plugin_tags","embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_tags?post=14175"},{"taxonomy":"plugin_category","embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_category?post=14175"},{"taxonomy":"plugin_contributors","embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_contributors?post=14175"},{"taxonomy":"plugin_business_model","embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_business_model?post=14175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}