如何设置自定义帖子类型以具有可查看的未来帖子

时间:2012-03-02 作者:Scott

我已经设置了一个CPT,其作用与帖子相同,但用于发布事件详细信息。

问题是,有些帖子是在未来发布的,而且这些帖子上有一个未来的日期。问题是普通用户看不到这些帖子。

因此:

如何更改存档事件。php也将列出未来的帖子?在保持分页的同时,首先显示将来的文章,最后显示最旧的文章

3 个回复
最合适的回答,由SO网友:Scott 整理而成

我自己就能解决这个问题。注册CPT的完整代码:

<?php
add_action( \'init\', \'events_post_type_register\' );
function events_post_type_register() {

    $post_type = "events";

    $labels = array(
        \'name\' => _x(\'Events\', \'post type general name\', \'project_X\'),
        \'singular_name\' => _x(\'Event\', \'post type singular name\', \'project_X\'),
        \'add_new\' => _x(\'Add New\', \'event\', \'project_X\'),
        \'add_new_item\' => __(\'Add New Event\', \'project_X\'),
        \'edit_item\' => __(\'Edit Event\', \'project_X\'),
        \'new_item\' => __(\'New Event\', \'project_X\'),
        \'all_items\' => __(\'All Events\', \'project_X\'),
        \'view_item\' => __(\'View Event\', \'project_X\'),
        \'search_items\' => __(\'Search Events\', \'project_X\'),
        \'not_found\' =>  __(\'No events found\', \'project_X\'),
        \'not_found_in_trash\' => __(\'No events found in trash\', \'project_X\'),
        \'parent_item_colon\' => \'\',
        \'menu_name\' => \'Events\'
    );

    $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'hierarchical\' => false,
        \'has_archive\' => true,
        \'rewrite\' => array(
            \'with_front\' => false,
            \'slug\' => "news/{$post_type}"
        ),
        \'supports\' => array( \'title\', \'editor\', \'thumbnail\' )
    );
    register_post_type($post_type, $args);

    remove_action("future_{$post_type}", \'_future_post_hook\');
    add_action("future_{$post_type}", \'sc_ps_publish_future_events_now\', 2, 10);
}

function sc_ps_publish_future_events_now($depreciated, $post) {
    wp_publish_post($post);
}

add_filter(\'posts_where\', \'sc_ps_show_future_events_where\', 2, 10);
function sc_ps_show_future_events_where($where, $that) {
    global $wpdb;
    if("events" == $that->query_vars[\'post_type\'] && is_archive())
        $where = str_replace( "{$wpdb->posts}.post_status = \'publish\'", "{$wpdb->posts}.post_status = \'publish\' OR $wpdb->posts.post_status = \'future\'", $where);
    return $where;
}
?>
因此,要允许所有用户都能看到帖子,即使这些帖子是在将来设置的,您需要执行以下操作:

remove_action("future_{$post_type}", \'_future_post_hook\');
add_action("future_{$post_type}", \'sc_ps_publish_future_events_now\', 2, 10);
我们删除了处理稍后发布的操作,并应用我们自己的操作强制发布,尽管它的未来日期为:

wp_publish_post($post);
然后,我们现在需要做的就是通过过滤在归档页面上显示未来的帖子posts_where:

function sc_ps_show_future_events_where($where, $that) {
    global $wpdb;
    if("events" == $that->query_vars[\'post_type\'] && is_archive())
        $where = str_replace( "{$wpdb->posts}.post_status = \'publish\'", "{$wpdb->posts}.post_status = \'publish\' OR $wpdb->posts.post_status = \'future\'", $where);
    return $where;
}

SO网友:Zade

Brady,我非常感谢你引导我找到这个解决方案。我的客户已经在没有自定义字段的情况下设置了所有活动日期,我不打算回去更改所有内容。您的代码最初在尝试发布时抛出了一个错误,但经过了以下细微的修改(与wp includes/post.php中使用的格式相匹配):

remove_action( \'future_\' . $post_type, \'_future_post_hook\', 5, 2 );
add_action( \'future_\' . $post_type, \'my_future_post_hook\', 5, 2);
以及

function my_future_post_hook( $deprecated = \'\', $post ) {
    wp_publish_post( $post->ID );
}
我花了一段时间想弄明白这一点。希望它能帮助别人!

SO网友:Joe

在不更改帖子状态的情况下,您可以单独显示未来的帖子,也可以使用pre\\u get\\u帖子进行存档:

add_action( \'pre_get_posts\', \'joesz_include_future_posts\' );
function joesz_include_future_posts( $query ) {

    if ( $query->is_main_query() && 
           ( $query->query_vars[\'post_type\'] == \'your-post-type\' || // for single
         is_post_type_archive( \'your-post-type\' ) ) ) {         // for archive

        $query->set( \'post_status\', array( \'future\', \'publish\' ) );

    }

}

结束

相关推荐

Only Showing Upcoming Events

在此页面的侧栏中:http://lifebridgecypress.org/our-people, 我有一个即将使用此代码的事件列表。。。<ul id=\"upcoming-events\"> <?php $latestPosts = new WP_Query(); $latestPosts->query(\'cat=3&showposts=10\'); ?> <?php while ($latestPos

如何设置自定义帖子类型以具有可查看的未来帖子 - 小码农CODE - 行之有效找到问题解决它

如何设置自定义帖子类型以具有可查看的未来帖子

时间:2012-03-02 作者:Scott

我已经设置了一个CPT,其作用与帖子相同,但用于发布事件详细信息。

问题是,有些帖子是在未来发布的,而且这些帖子上有一个未来的日期。问题是普通用户看不到这些帖子。

因此:

如何更改存档事件。php也将列出未来的帖子?在保持分页的同时,首先显示将来的文章,最后显示最旧的文章

3 个回复
最合适的回答,由SO网友:Scott 整理而成

我自己就能解决这个问题。注册CPT的完整代码:

<?php
add_action( \'init\', \'events_post_type_register\' );
function events_post_type_register() {

    $post_type = "events";

    $labels = array(
        \'name\' => _x(\'Events\', \'post type general name\', \'project_X\'),
        \'singular_name\' => _x(\'Event\', \'post type singular name\', \'project_X\'),
        \'add_new\' => _x(\'Add New\', \'event\', \'project_X\'),
        \'add_new_item\' => __(\'Add New Event\', \'project_X\'),
        \'edit_item\' => __(\'Edit Event\', \'project_X\'),
        \'new_item\' => __(\'New Event\', \'project_X\'),
        \'all_items\' => __(\'All Events\', \'project_X\'),
        \'view_item\' => __(\'View Event\', \'project_X\'),
        \'search_items\' => __(\'Search Events\', \'project_X\'),
        \'not_found\' =>  __(\'No events found\', \'project_X\'),
        \'not_found_in_trash\' => __(\'No events found in trash\', \'project_X\'),
        \'parent_item_colon\' => \'\',
        \'menu_name\' => \'Events\'
    );

    $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'hierarchical\' => false,
        \'has_archive\' => true,
        \'rewrite\' => array(
            \'with_front\' => false,
            \'slug\' => "news/{$post_type}"
        ),
        \'supports\' => array( \'title\', \'editor\', \'thumbnail\' )
    );
    register_post_type($post_type, $args);

    remove_action("future_{$post_type}", \'_future_post_hook\');
    add_action("future_{$post_type}", \'sc_ps_publish_future_events_now\', 2, 10);
}

function sc_ps_publish_future_events_now($depreciated, $post) {
    wp_publish_post($post);
}

add_filter(\'posts_where\', \'sc_ps_show_future_events_where\', 2, 10);
function sc_ps_show_future_events_where($where, $that) {
    global $wpdb;
    if("events" == $that->query_vars[\'post_type\'] && is_archive())
        $where = str_replace( "{$wpdb->posts}.post_status = \'publish\'", "{$wpdb->posts}.post_status = \'publish\' OR $wpdb->posts.post_status = \'future\'", $where);
    return $where;
}
?>
因此,要允许所有用户都能看到帖子,即使这些帖子是在将来设置的,您需要执行以下操作:

remove_action("future_{$post_type}", \'_future_post_hook\');
add_action("future_{$post_type}", \'sc_ps_publish_future_events_now\', 2, 10);
我们删除了处理稍后发布的操作,并应用我们自己的操作强制发布,尽管它的未来日期为:

wp_publish_post($post);
然后,我们现在需要做的就是通过过滤在归档页面上显示未来的帖子posts_where:

function sc_ps_show_future_events_where($where, $that) {
    global $wpdb;
    if("events" == $that->query_vars[\'post_type\'] && is_archive())
        $where = str_replace( "{$wpdb->posts}.post_status = \'publish\'", "{$wpdb->posts}.post_status = \'publish\' OR $wpdb->posts.post_status = \'future\'", $where);
    return $where;
}

SO网友:Zade

Brady,我非常感谢你引导我找到这个解决方案。我的客户已经在没有自定义字段的情况下设置了所有活动日期,我不打算回去更改所有内容。您的代码最初在尝试发布时抛出了一个错误,但经过了以下细微的修改(与wp includes/post.php中使用的格式相匹配):

remove_action( \'future_\' . $post_type, \'_future_post_hook\', 5, 2 );
add_action( \'future_\' . $post_type, \'my_future_post_hook\', 5, 2);
以及

function my_future_post_hook( $deprecated = \'\', $post ) {
    wp_publish_post( $post->ID );
}
我花了一段时间想弄明白这一点。希望它能帮助别人!

SO网友:Joe

在不更改帖子状态的情况下,您可以单独显示未来的帖子,也可以使用pre\\u get\\u帖子进行存档:

add_action( \'pre_get_posts\', \'joesz_include_future_posts\' );
function joesz_include_future_posts( $query ) {

    if ( $query->is_main_query() && 
           ( $query->query_vars[\'post_type\'] == \'your-post-type\' || // for single
         is_post_type_archive( \'your-post-type\' ) ) ) {         // for archive

        $query->set( \'post_status\', array( \'future\', \'publish\' ) );

    }

}