0

I'm trying to utilize existing post-grid functions to show attachment posts but $post_status is hard coded to be either 'publish' or 'private' whereas attachments have a status of inherit. I added 'inherit' to their source code to confirm it works but I am trying to achieve this without modifying the source.

There are a couple layers of methods in the Class before it renders so I'm having trouble figuring out how I can filter $post_status to 'inherit'.

This is what I'm trying to do:

This works when I use $args for anything but attachments.

function my_attachment_grid() {
    $args = [
        'post_type' => 'attachment',
        'post_status' => 'inherit',
    ];
    do_grid( 'post', $args );
}

This is what I'm working with:

Here is the function (outside of Class). If I var_dump($grid) before $grid->render(), I can see all the default $args but $query_args is empty. $post_status is not listed here.

function do_grid( $type, $args = [] ) {
    $args = array_merge( [ 'type' => $type ], $args );
    $grid = new Mai_Grid( $args );
    $grid->render();
}

The Class itself:

Notice get_post_query_args() declares $post_status as 'publish', assigns it to $query_args, then apply_filters(). But as mentioned above, $query_args is protected and empty in a new Object.

I believe I should be able add_filter() but I'm not sure if I'm calling it correctly or if it's even possible since it's hard coded within and not showing within the default args.

class Grid {
    // all protected 
    public function __construct( $args ) {
        $args['context']  = 'block'; // Required for Mai_Entry.
        $this->type       = isset( $args['type'] ) ? $args['type'] : 'post';
        $this->defaults   = $this->get_defaults();
        $this->args       = $this->get_sanitized_args( $args );
        $this->query_args = [];
    }

    public function render() {
        $this->query = $this->get_query();
        // ...
        do_entries_open( $this->args ); // Open.
        $this->do_grid_entries(); // Entries.
        do_entries_close( $this->args ); // Close.
    }

    public function get_query() { // modify if post query or term query
        $this->query_args = $this->get_post_query_args();
        return $query;
    }

    public function get_post_query_args() {
        $post_status = 'publish';
        $query_args = [
            'post_status' => $post_status,
            //..
        ]
        // ...
        return apply_filters( 'post_grid_query_args', $query_args, $this->args );
    }
}

1 Answer 1

0

Sometimes it takes asking a question to find out the answer.

function my_attachment_grid() {
    $args = [
        'post_type' => 'attachment',
    ];
    add_filter( 'post_query_args', 'my_filter', 10, 1 );
    do_grid( 'post', $args );
}

function my_filter( $args ) {
    
    $defaults = ['post_status'=>'inherit'];
    $args = array_merge( $args, $defaults );

    return $args;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.