0
votes

I'm stuck on something that I'm sure must be pretty simple but is getting me nuts. I'm forced at work to use WordPress, which I have zero experience with, and I'm struggling so far to understand how it operates regarding hooks and filters.

What I want is pretty simple:
I'm using the latest posts block to display posts written by users. Except that the page I'm working on would be the front end for the website's moderator who would have to see the posts with the 'pending' status, not the 'publish' one. I couldn't find any option to change that in the editor, so I'm trying to set a hook to change the query from 'post_status' => 'publish' to 'post_status' => 'pending', but it's not working, I get a 'Oops! That page can’t be found.'

Here's what I wrote in my functions.php:


    function name_of_the_function( $query ) {
        if( get_query_var('pagename') == 'name_of_the_page' && current_user_can('publish_posts') && $query->is_main_query() ) {
            $query->set( 'post_status', 'pending' );
            return $query;
        }
    }
    add_filter( 'pre_get_posts', 'name_of_the_function' );

If I leave this function exactly like that but write 'publish' instead of 'pending' the page displays correctly the last published posts, but with 'pending' I get the message I mentioned before. And I tried with add_action instead of add_filter and got hte same results.
I'd like to add that I do have pending posts awaiting, and if I write the following in my page template, they are found:


    $args = array (
        'cat'           => 5,
        'post_status'   => 'pending'
    
    );
    $query = new WP_Query( $args );
    while ( $query->have_posts() ) {
        $query->the_post();
        echo get_the_title();
    }



Just to check, directly in the wp-includes/latest-posts.php file, I changed :


    $args = array(
            'posts_per_page'   => $attributes['postsToShow'],
            'post_status'      => 'publish',
            'order'            => $attributes['order'],
            'orderby'          => $attributes['orderBy'],
            'suppress_filters' => false,
        );


to :

 

    $args = array(
            'posts_per_page'   => $attributes['postsToShow'],
            'post_status'      => 'pending',
            'order'            => $attributes['order'],
            'orderby'          => $attributes['orderBy'],
            'suppress_filters' => false,
        );


It works and displays the pending posts but of course I can't use that as the file would be erased at every WordPress update.
Sorry for the long post but I'm lost now and don't know what else to do, I've looked all other the intrnet but can't find an answer to this, I would really appreciate any help regarding that matter, thanks in advance.

1

1 Answers

0
votes

By using the pre_get_posts with $query->is_main_query(), you apply this to the query that WordPress uses to find the page (the main query). Your code needs to be changed to:

function name_of_the_function( $query ) {
    if ( is_admin() || $query->is_main_query() ) {
        return $query;
    }

    if( get_query_var('pagename') == 'name_of_the_page' && current_user_can('publish_posts') ) {
        $query->set( 'post_status', 'pending' );
    }

    return $query;
}
add_filter( 'pre_get_posts', 'name_of_the_function' );

So basically, don't run this on any query in the admin or any query that is the main query, but only run it on a specific page for people with specific capabilities.