Post

Single post/page entity

Properties & methods

PropertyType

ID

Post ID

int

ancestor

Retrieves the root ancestor of a post

object

ancestors(reverse)

Retrieves the ancestors of a post

array

author

Post author as a User

object

blocks

Parses blocks out of a content string.

array

children(order)

Get post children

array

class

Retrieves the class names for the post container element.

string

classes

Retrieves the class names for the post container element.

array

comment_count

Post comment total count

int

comments

Perform a WP_Comment_Query

object

comment_status

Post comment status

string

content(index)

Post content, if post has blocks, return a parsed content as string, useful to create search index for instance

string

custom_fields(name)

Get ACF value

mixed

date(format)

Formatted post created date, see mysql2date

string

date_gmt(format)

Formatted post created gmt date, see mysql2date

string

excerpt

Post excerpt, if empty generate one from content

string

link

Post url

string

menu_order

Post menu order from Backend

int

meta(name)

Get post meta

mixed

modified(format)

Formatted post modified date, see mysql2date

string

modified_gmt(format)

Formatted post modified gmt date, see mysql2date

string

next(in_same_term , excluded_terms, tax)

Retrieves the next post that is adjacent to the current post, see get_next_post

object

parameters

Get post url parameters based on rewrite rules

object

parent

Post parent

object

password

post password

string

path

Relative post link without prefix

string

prev

Retrieves the previous post that is adjacent to the current post, see get_previous_post

object

siblings(order)

Retrieves all posts with the same post parent

array

slug

Post slug

string

state

Post state, based on 'page_on_*' option

string

status

Post status

string

taxonomies(public)

Returns the objects of the taxonomies which are registered for the post type

array

template

Post template slug

string

term(tax, args)

Primary term

object

terms(tax, args, order, limit)

Perform a WP_Term_Query, return TermCollection

object

thumbnail

Retrieves thumbnail

object

title

Post title

string

type(property)

Return post type, use property param to get specific post type object property

mixed

has_block(name)

Check if post has a specific block ( Gutenberg )

bool

has_blocks

Check if post has blocks ( Gutenberg )

bool

has_children

Count post children

bool

has_excerpt

Excerpt has been filled

bool

has_parent

Post has parent

bool

is_current

Post is currently viewed

bool

is_public

Post is public

bool

is_sticky

Post is sticky

bool

meta

Get post meta value

mixed

Dates are formatted using date_format option, add false as parameter to return raw date : $post->getDate(false) or {{ post.date(false) }}

Examples

Check for a specific block

Useful when a block needs a specific code in the layout Example: load recaptcha library if post contains the "contact-form" acf block

<!DOCTYPE html>
<html>
<body>

    <div class="sections">
        {% block body %}{% endblock %}
    </div>

    {% if post|default and post.hasBlock('acf/contact-form') %}
        <script src="https://www.recaptcha.net/recaptcha/api.js?lang={{ blog.locale }}"></script>
        <script>
            function timestamp() { var response = document.getElementById("g-recaptcha-response"); if (response == null || response.value.trim() == "") {var elems = JSON.parse(document.getElementsByName("captcha_settings")[0].value);elems["ts"] = JSON.stringify(new Date().getTime());document.getElementsByName("captcha_settings")[0].value = JSON.stringify(elems); } } setInterval(timestamp, 500);
            function recaptcha_callback(){ document.getElementById('contact-submit').disabled = false }
        </script>
    {% endif %}

Display post meta

see https://developer.wordpress.org/reference/functions/get_post_meta/

{{ post.meta.my_field }}

Display ACF fields

Display a repeater fields with position and title sub-fields see https://www.advancedcustomfields.com/resources/get_field/

{% for item in post.custom_fields.items %}
    {{ item.position }} {{ item.title }}
{% endfor %} 

Display thumbnail

<a href="{{ post.link }}">
    <img src="{{ post.thumbnail(467,251) }}" alt="{{ post.thumbnail.alt }}"/>
</a>

or using picture twig filter

<a href="{{ post.link }}">
    {{ post.thumbnail|picture(467,251,{'min-width: 1025px':[399,215]}) }}
</a>

Display blocks

{% for block in post.blocks %}
    {% include 'block/'~block.name~'.html.twig' with{ props: block.content } %}
{% endfor %}

Show comments : $args=['status'=>'approve', number=>5]

{% if post.comment_count %}
  <ol class="comment-list">
    {% for comment in post.comments %}
      <li id="comment-{{ comment.ID }}" class="comment {{ loop.index is odd ? 'odd thread-odd' : 'even thread-even' }} depth-1 parent">
        {% include 'template-parts/content/content-comment.html.twig' with  { depth: 1 } %}
      </li>
    {% endfor %}
  </ol><!-- .comment-list -->
{% endif %}

Show ancestors

{% for parent in post.ancestors %}
  <span class="parent-links">
    <a href="{{ parent.link }}">{{ parent.title }}</a>
  </span>
 {% endfor %}

Query term

{% if post.term('custom_tax') %}
  <span class="cat-links">
    <a href="{{ post.term.link }}">{{ post.term.title }}</a>
  </span>
 {% endif %}

Query terms : $criteria=[], array $orderBy = null, $limit = null, $offset = null

When using Yoast SEO, primary term will be the first item of the array

{% for term in post.terms('custom_tax') %}
  <span class="cat-links">
    <a href="{{ term.link }}">{{ term.title }}</a>
  </span>
 {% endfor %}

Last updated