Wordpress Future Page Issue (Not Showing)

2019-09-12 20:56发布

问题:

I've been asked to fix a problem on a wordpress site. The problem is caused by the fact posts should of been used instead of pages. However the site is up and running and quickest option would be to fix the problem using a hack of sorts.

The site has an events section, each event is a page (this is the issue, should really be a post). In order to have upcoming and past events the post date is used, therefore upcoming events has a post status of 'future'.

There's are list page of events which shows them fine by using the correct query_post(). Although the problem arises when you click through to actual event (which is a future page). If your logged in as an Admin then the page shows but if your not logged in you get a 404 page.

Now if they where posts then the "The Future is Now!" plugin would solve this problem. I have a feeling the only way round the problem is to rewrite part of core Wordpress files.

Any advice would be great, I have a lot of experience with Wordpress so even if you can point me in the right direction.

Cheers, Jason

[Update]

Thanks maiorano84 for your detailed response. In the long run I intend to move them to posts but in the meantime they've requested we fix it asap without changing any urls (today they sent out a mass email with a list of events without checking any links)

Your solution of including post_status future doesn't work in this case as wordpress doesn't get to the stage of loading the template. Something in wordpress core stops it from getting that far. Ideally if they was a hook I could use to override this behavior in the meantime that would be excellent but if it comes to it I will temporarily edit the core files.

[Update 2]

I now know the two functions that need editing or use a hook that relates to them.

First of we is_404() which needs changed to not add future pages as 404

Second we have is_page() need to return true if a page is future

[Update 3]

I've found how to do this in the core. If you go to wp-includes/query.php line 2682. Copy this in instead of the old function it works correct. If you have a better way please let me know. Thanks.

        /** Future Pages **/
    // Check post status to determine if post should be displayed.
    if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
        $status = get_post_status($this->posts[0]);
        $post_status_obj = get_post_status_object($status);;
        if ( !$post_status_obj->public ) {
                if  ( $post_status_obj->protected ) {
                        $this->is_preview = true;
                        print_r($status);
                        if ( 'draft' != $status ) {
                            $this->posts[0]->post_date = current_time('mysql');
                            } else {
                            $this->posts = array();
                        }
                } elseif ( $post_status_obj->private ) {
                    if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
                        $this->posts = array();
                } else {
                    $this->posts = array();
                }
        }
        /** END **/

回答1:

You can actually add a post_status parameter of 'future' to your page queries. You REALLY shouldn't be modifying your core files in order to do what you want. So on your page.php and archive.php templates (or other relevant templates that's controlling your Event display), you can do something like this:

<?php
$params = array('posts_per_page'=>-1, 'post_status'=>'publish,future');
$query = new WP_Query($params);
if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
?>
<!-- YOUR PAGE HTML HERE -->
<?php
endwhile;endif;
wp_reset_postdata();
?>

This is an oversimplification, but using the correct queries in the corresponding files will allow you to display your pages however you like.

More information here: http://codex.wordpress.org/Class_Reference/WP_Query

Another thing worth considering, and I realize that this wasn't a part of your question, but may very well solve your issue:

Why not create a subdomain on your client's server where you can work on fixing everything without interrupting the user experience? You can easily either import the existing database into your development environment, and make all the changes you need without affecting the live version.

Food for thought. My advice would be to nip this in the bud, and convert the pages over to posts as soon as possible, otherwise the website will turn into a giant mess very quickly, but that's your call.

Hope this helps.

UPDATE:

I would still advise against altering the core files, but if it's a last resort until everything gets fixed, then have at it. Here's is a "WP Friendly" solution I think might help:

add_action('wp','future_redirect', 0);
function future_redirect($WP_Object)
{
    $page = is_page() ? get_page(get_the_ID()) : false;
    if($page && $page->post_status == 'future')
    {
        wp_redirect(/*YOUR REDIRECT URL*/);
        exit();
    }
    return $WP_Object;
}