# Post

### Properties & methods

<table><thead><tr><th width="212.2">Property</th><th width="441"></th><th>Type</th></tr></thead><tbody><tr><td>ID</td><td>Post ID</td><td>int</td></tr><tr><td>ancestor</td><td>Retrieves the root ancestor of a post</td><td>object</td></tr><tr><td>ancestors(reverse)</td><td>Retrieves the ancestors of a post</td><td>array</td></tr><tr><td>author</td><td>Post author as a <a href="user">User</a></td><td>object</td></tr><tr><td>blocks</td><td>Parses blocks out of a content string.</td><td>array</td></tr><tr><td>children(<em>order</em>)</td><td>Get post children</td><td>array</td></tr><tr><td>class</td><td>Retrieves the class names for the post container element.</td><td>string</td></tr><tr><td>classes</td><td>Retrieves the class names for the post container element.</td><td>array</td></tr><tr><td>comment_count</td><td>Post comment total count</td><td>int</td></tr><tr><td>comments</td><td>Perform a WP_Comment_Query</td><td>object</td></tr><tr><td>comment_status</td><td>Post comment status</td><td>string</td></tr><tr><td>content(index)</td><td>Post content, if post has blocks, return a parsed content as string, useful to create search index for instance</td><td>string</td></tr><tr><td>custom_fields(<em>name</em>)</td><td>Get ACF value</td><td>mixed</td></tr><tr><td>date(<em>format</em>)</td><td>Formatted post created date, see <a href="https://developer.wordpress.org/reference/functions/mysql2date/">mysql2date</a></td><td>string</td></tr><tr><td>date_gmt(<em>format</em>)</td><td>Formatted post created gmt date, see <a href="https://developer.wordpress.org/reference/functions/mysql2date/">mysql2date</a></td><td>string</td></tr><tr><td>excerpt</td><td>Post excerpt, if empty generate one from content</td><td>string</td></tr><tr><td>link</td><td>Post url</td><td>string</td></tr><tr><td>menu_order</td><td>Post menu order from Backend</td><td>int</td></tr><tr><td>meta(<em>name</em>)</td><td>Get post meta</td><td>mixed</td></tr><tr><td>modified(<em>format</em>)</td><td>Formatted post modified date, see <a href="https://developer.wordpress.org/reference/functions/mysql2date/">mysql2date</a></td><td>string</td></tr><tr><td>modified_gmt(<em>format</em>)</td><td>Formatted post modified gmt date, see <a href="https://developer.wordpress.org/reference/functions/mysql2date/">mysql2date</a></td><td>string</td></tr><tr><td>next(<em>in_same_term , excluded_terms, tax</em>)</td><td>Retrieves the next post that is adjacent to the current post, see <a href="https://developer.wordpress.org/reference/functions/get_next_post/">get_next_post</a></td><td>object</td></tr><tr><td>parameters</td><td>Get post url parameters based on rewrite rules</td><td>object</td></tr><tr><td>parent</td><td>Post parent</td><td>object</td></tr><tr><td>password</td><td>post password</td><td>string</td></tr><tr><td>path</td><td>Relative post link without prefix</td><td>string</td></tr><tr><td>prev</td><td>Retrieves the previous post that is adjacent to the current post, see <a href="https://developer.wordpress.org/reference/functions/get_previous_post/">get_previous_post</a></td><td>object</td></tr><tr><td>siblings(<em>order</em>)</td><td>Retrieves all posts with the same post parent</td><td>array</td></tr><tr><td>slug</td><td>Post slug</td><td>string</td></tr><tr><td>state</td><td>Post state, based on 'page_on_*' option</td><td>string</td></tr><tr><td>status</td><td>Post status</td><td>string</td></tr><tr><td>taxonomies(<em>public</em>)</td><td>Returns the objects of the taxonomies which are registered for the post type</td><td>array</td></tr><tr><td>template</td><td>Post template slug</td><td>string</td></tr><tr><td>term(<em>tax,</em> args)</td><td>Primary term</td><td>object</td></tr><tr><td>terms(<em>tax,</em> args, order, limit)</td><td>Perform a WP_Term_Query, return TermCollection</td><td>object</td></tr><tr><td>thumbnail</td><td>Retrieves thumbnail</td><td>object</td></tr><tr><td>title</td><td>Post title</td><td>string</td></tr><tr><td>type(<em>property</em>)</td><td>Return post type, use property param to get specific post type object property</td><td>mixed</td></tr><tr><td>has_block(<em>name</em>)</td><td>Check if post has a specific block ( Gutenberg )</td><td>bool</td></tr><tr><td>has_blocks</td><td>Check if post has blocks ( Gutenberg )</td><td>bool</td></tr><tr><td>has_children</td><td>Count post children</td><td>bool</td></tr><tr><td>has_excerpt</td><td>Excerpt has been filled</td><td>bool</td></tr><tr><td>has_parent</td><td>Post has parent</td><td>bool</td></tr><tr><td>is_current</td><td>Post is currently viewed</td><td>bool</td></tr><tr><td>is_public</td><td>Post is public</td><td>bool</td></tr><tr><td>is_sticky</td><td>Post is sticky</td><td>bool</td></tr><tr><td>meta</td><td>Get post meta value</td><td>mixed</td></tr></tbody></table>

{% hint style="info" %}
Dates are formatted using `date_format` option, add false as parameter to return raw date : `$post->getDate(false)` or `{{ post.date(false) }}`
{% endhint %}

## Examples

#### Check for a specific block

Useful when a block needs a specific code in the layout\
\
\&#xNAN;*Example: load recaptcha library if post contains the "contact-form" acf block*

```twig
<!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/*](https://developer.wordpress.org/reference/functions/get_post_meta/)

```twig
{{ post.meta.my_field }}
```

#### Display ACF fields

Display a repeater fields with position and title sub-fields\
\&#xNAN;*see* [*https://www.advancedcustomfields.com/resources/get\_field/*](https://www.advancedcustomfields.com/resources/get_field/)

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

#### Display thumbnail

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

or using picture twig filter

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

#### Display blocks

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

#### Show comments : [$args](https://developer.wordpress.org/reference/classes/wp_comment_query/__construct/)=\['status'=>'approve', number=>5]

```liquid
{% 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

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

#### Query term

```liquid
{% 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](https://yoast.com/wordpress/plugins/seo/), primary term will be the first item of the array

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