Post

A better post object. Ditch "The Loop".

And in case you missed it, every template gets passed a $Post and $Posts variable.

Read more about this in the Default Data section.

Usage

Single
    
            <?php /** @var \WPDev\Models\Post $Post */ ?>
<h1>
	<a href="<?= $Post->url(); ?>">
		<?= $Post->title(); ?>
	</a>
</h1>
<?= $Post->content(); ?>
    
Loop
    
            <?php /** @var \WPDev\Models\Post[] $Posts */ ?>
<?php foreach ($Posts as $post) : ?>
	<div>
		<?= $post->featuredImage('medium'); ?>
		<h2><?= $post->title(); ?></h2>
		<div><?= $post->excerpt(); ?></div>
		<a href="<?= $post->url(); ?>">Read More</a>
	</div>
<?php endforeach; ?>
    

API Reference

Public Methods
Method Summary
__construct Constructor. For a more fluid syntax use Post::create().
create
static
Alternative to constructor. For more fluid syntax.
acfField Uses ACF's get_field() to fetch a field value.
acfFields Uses ACF's get_fields() to get all custom field values in an associative array.
ancestors Retrieves the parent posts in direct parent to highest level ancestor order. Each post will be an instance of \WPDev\Models\Post.
createdDate The date the post was created.
featuredImage Get the featured image. Returns an instance of \WPDev\Models\Image.
featuredImageId Get the featured image id.
field Gets a field value using get_post_meta().
hasFeaturedImage Whether the post has a featured image or not.
hasTerm Checks whether the post has the term. If no arg passed checks if it has any term.
id Post ID
title Post title
url Post url (aka permalink).
content Same as the_content() except we don't echo.
excerpt Post excerpt
modifiedDate The last modified date.
parent Gets the parent post if any. If so will return a \WPDev\Models\Post object.
parentId The parent ID if there is a parent. Else 0.
postType The post type
status The current post status
taxonomies The taxonomy names associated with the post.
terms Terms for all or a specific associated taxonomy.
wpPost The original WP_Post object
  • __construct

    __construct( int | \WP_Post | null $post )

    Summary
    Constructor. For a more fluid syntax use Post::create().
    Parameters
    post
    Optional. Post ID or post object. Defaults to global $post.
  • create

    create( int | \WP_Post | null $post )

    Summary
    Alternative to constructor. For more fluid syntax.
    Parameters
    post
    Optional. Post ID or post object. Defaults to `$GLOBALS['post']`.
    Returns

    $this

  • acfField

    acfField( string $selector [, bool $format ] )

    Summary
    Uses ACF's get_field() to fetch a field value.
    Parameters
    selector
    The field key
    format
    Should ACF format the value for you
    Returns

    mixed | null

  • acfFields

    acfFields( [ bool $format ] )

    Summary
    Uses ACF's get_fields() to get all custom field values in an associative array.
    Parameters
    format
    Whether ACF should format the values.
    Returns

    array

    Associate array with all custom field values

  • ancestors

    ancestors( )

    Summary
    Retrieves the parent posts in direct parent to highest level ancestor order. Each post will be an instance of \WPDev\Models\Post.
    Returns

    \WPDev\Models\Post[]

    Array of \WPDev\Models\Post objects

  • createdDate

    createdDate( [ string $date_format ] )

    Summary
    The date the post was created.
    Parameters
    date_format
    A date format string. Defaults to `get_option('date_format')` date format set in the WP backend.
    Returns

    bool | string

    The formatted date. False on failure.

  • featuredImage

    featuredImage( [ string $size ] )

    Summary
    Get the featured image. Returns an instance of \WPDev\Models\Image.
    Parameters
    size
    The image size to use.
    Returns

    \WPDev\Models\Image

  • featuredImageId

    featuredImageId( )

    Summary
    Get the featured image id.
    Returns

    int

  • field

    field( string $key [, bool $single_value ] )

    Summary
    Gets a field value using get_post_meta().
    Parameters
    key
    The field key (aka meta key).
    single_value
    Whether WP should return the value or the value wrapped in an array.
    Returns

    mixed

    The field value if it exists. Else an empty string if $single_value = true or an empty array if $single_value = false.

  • hasFeaturedImage

    hasFeaturedImage( )

    Summary
    Whether the post has a featured image or not.
    Returns

    bool

  • hasTerm

    hasTerm( [ string | int | array $term, string $taxonomy_name ] )

    Summary
    Checks whether the post has the term. If no arg passed checks if it has any term.
    Parameters
    term
    The term name/term_id/slug or array of them to check for.
    taxonomy_name
    Taxonomy name
    Returns

    bool

    True if the current post has any of the given tags (or any tag, if no tag specified).

  • id

    id( )

    Summary
    Post ID
    Returns

    int

  • title

    title( )

    Summary
    Post title
    Returns

    string

  • url

    url( )

    Summary
    Post url (aka permalink).
    Returns

    bool | string

  • content

    content( null $more_link_text [, bool $strip_teaser ] )

    Summary
    Same as the_content() except we don't echo.
    Parameters
    more_link_text
    strip_teaser
    Returns

    string

  • excerpt

    excerpt( )

    Summary
    Post excerpt
    Returns

    string

  • modifiedDate

    modifiedDate( [ string $date_format ] )

    Summary
    The last modified date.
    Parameters
    date_format
    Returns

    bool | string

  • parent

    parent( )

    Summary
    Gets the parent post if any. If so will return a \WPDev\Models\Post object.
    Returns

    bool | \WPDev\Models\Post

  • parentId

    parentId( )

    Summary
    The parent ID if there is a parent. Else 0.
    Returns

    int

  • postType

    postType( )

    Summary
    The post type
    Returns

    string

  • status

    status( )

    Summary
    The current post status
    Returns

    bool | string

  • taxonomies

    taxonomies( )

    Summary
    The taxonomy names associated with the post.
    Returns

    array

    Taxonomy names

  • terms

    terms( null $taxonomy_name )

    Summary
    Terms for all or a specific associated taxonomy.
    Parameters
    taxonomy_name
    Returns

    array | mixed

  • wpPost

    wpPost( )

    Summary
    The original WP_Post object
    Returns

    \WP_Post | null