$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>functions - How to make custom posts types display when published for the future|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - How to make custom posts types display when published for the future

matteradmin10PV0评论

I'm having a hard time accomplishing two things. I've created a custom post type for Events. I would like for the client to be able to post the event by the date. This means publishing posts that are scheduled for the future to appear published. I would also like the posts to revert back to drafts once the date has passed. Anyone know how to accomplish this? Here's the code I've been toying with:

<?php 
// Register Custom Post Type: Events Posts

add_action('init', 'events_register');

function events_register() {

    $labels = array(
        'name' => _x('Events', 'post type general name'),
        'singular_name' => _x('Event', 'post type singular name'),
        'add_new' => _x('Add New', 'Event item'),
        'add_new_item' => __('Add New Event'),
        'edit_item' => __('Edit Event'),
        'new_item' => __('New Event'),
        'view_item' => __('View Event Item'),
        'search_items' => __('Search Events'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'menu_icon' => 'dashicons-calendar-alt',
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => 4,
        'supports' => array('title','editor','thumbnail')
      ); 

    register_post_type( 'events' , $args );

    function setup_future_hook() {
        // Replace native future_post function with replacement
            remove_action('future_events','_future_post_hook');
            add_action('future_events','publish_future_post_now');
        }

        function publish_future_post_now($id) {
        // Set new post's post_status to "publish" rather than "future."
            wp_publish_post($id);
        }

        add_action('init', 'setup_future_hook');

}

?>

This is my loop:

<?php // WP_Query arguments
                $args = array (
                'post_type'              => array( 'events' ),
                'order'                  => 'ASC',
                'posts_per_page'         => '4',
                );

                // The Query
                $query = new WP_Query( $args );

                // The Loop
                if ( $query->have_posts() ) {
                while ( $query->have_posts() ) {
                $query->the_post(); ?>
                <div class="col-md-6">
                    <div class="event-box">
                        <h4><?php the_title(); ?></h4>
                        <h5><?php the_field('venue_name'); ?></h5>
                        <div class="date"><?php the_date( 'F j, Y' ); ?></div> 
                        <div class="time"><?php the_field('event_time'); ?></div>
                        <div class="row">
                            <div class="col-6">
                                <a target="_blank" class="btn btn-primary" href="<?php the_field('buy_tickets_link'); ?>">Buy Tickets</a>
                            </div>
                            <div class="col-6">
                                <a target="_blank" class="btn btn-primary" href="<?php the_field('venue_link'); ?>">Venue Info</a>
                            </div>
                        </div>
                    </div>
                </div>

                <?php }
                } else {
                // no posts found
                }

                // Restore original Post Data
                wp_reset_postdata(); ?>

Thanks!

Post a comment

comment list (0)

  1. No comments so far