Post status - Dynamic Data

Hi guys,

It would be great i would be able to show post status for posts using dynamic data. Here is an use case. I let my user’s to post on my website, their posts gets automatically added as pending. It would be great if on their profile I would be able to show their pending post with a nice little status box.

Thanks!

2 Likes

Yes please! I’d like to be able to use Published status in a Query loop to build a Users Dashboard where they can see their posts and status of each.
Thx
Alan

You can do this simply by using an advanced “echo” tag with get_post_status() so it goes like {echo:get_post_status()} which will by default return current post status, but you can specify any post ID as per documentation.


On the front it renders like this

@alanj it works in a loop as well without any modification because then the default post is the current one inside of loop.

3 Likes

I completely forgot about the echo method. Thank you!

Remember to add get_post_status to the allowed Bricksbuilder functions for newer versions of Bricks

Hey guys,

until this gets included in core here is a small snippet to create a custom tag for the post status.

Usage:

  • {post_status}: Returns Draft / Pending Review / Private / Published
  • {post_status:value}: Returns draft / pending / private / publish

Snippet:

add_filter( 'bricks/dynamic_tags_list', 'add_my_tag_to_builder' );
function add_my_tag_to_builder( $tags ) {
    $tags[] = [
        'name'  => '{post_status}',
        'label' => __( 'Post status' ),
        'group' => 'post',
    ];

    return $tags;
}

add_filter( 'bricks/dynamic_data/render_tag', function( $tag ) {
    if ( ! preg_match( '/{post_status(:value)?}/', $tag ) ) {
        return $tag;
    }

    $post_status = get_post_status();

    if ( strpos( $tag, ':value' ) !== false ) {
        return $post_status;
    }

    $post_statuses = get_post_statuses();

    return $post_statuses[$post_status];
}, 20 );

add_filter( 'bricks/dynamic_data/render_content', 'render_post_status', 20 );
add_filter( 'bricks/frontend/render_data', 'render_post_status', 20 );
function render_post_status( $content ) {
    if ( ! preg_match( '/{post_status(:value)?}/', $content ) ) {
        return $content;
    }

    $post_statuses = get_post_statuses();
    $post_status = get_post_status();
    $post_status_label = $post_statuses[$post_status];

    return preg_replace_callback( '/{post_status(:value)?}/', function ( $matches ) use ( $post_status, $post_status_label ) {
        return empty( $matches[1] ) ? $post_status_label : $post_status;
    }, $content );
}

Let me know if it is working as expected or if I’m missing something.

Best,

André

2 Likes