setup_postdata example

// Fake being in the loop.         $wp_query->in_the_loop = true;

        // Fetch 20 posts at a time rather than loading the entire table into memory.         while ( $next_posts = array_splice( $post_ids, 0, 20 ) ) {
            $where = 'WHERE ID IN (' . implode( ',', $next_posts ) . ')';
            $posts = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} $where);

            // Begin Loop.             foreach ( $posts as $post ) {
                setup_postdata( $post );

                /** * Filters the post title used for WXR exports. * * @since 5.7.0 * * @param string $post_title Title of the current post. */
                $title = wxr_cdata( apply_filters( 'the_title_export', $post->post_title ) );

                /** * Filters the post content used for WXR exports. * * @since 2.5.0 * * @param string $post_content Content of the current post. */
return '';
    }

    // If this isn't the legacy block, we need to render the static version of this block.     $is_legacy = 'core/post-comments' === $block->name || ! empty( $attributes['legacy'] );
    if ( ! $is_legacy ) {
        return $block->render( array( 'dynamic' => false ) );
    }

    $post_before = $post;
    $post        = get_post( $post_id );
    setup_postdata( $post );

    ob_start();

    /* * There's a deprecation warning generated by WP Core. * Ideally this deprecation is removed from Core. * In the meantime, this removes it from the output. */
    add_filter( 'deprecated_file_trigger_error', '__return_false' );
    comments_template();
    remove_filter( 'deprecated_file_trigger_error', '__return_false' );

    

    public function prepare_item_for_response( $item$request ) {
        // Restores the more descriptive, specific name for use within this method.         $post            = $item;
        $GLOBALS['post'] = $post;

        setup_postdata( $post );

        $fields = $this->get_fields_for_response( $request );
        $data   = array();

        if ( in_array( 'author', $fields, true ) ) {
            $data['author'] = (int) $post->post_author;
        }

        if ( in_array( 'date', $fields, true ) ) {
            $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
        }

        

    public function prepare_item_for_response( $item$request ) {
        // Restores the more descriptive, specific name for use within this method.         $post            = $item;
        $GLOBALS['post'] = $post;

        setup_postdata( $post );

        $fields = $this->get_fields_for_response( $request );

        // Base fields for every post.         $data = array();

        if ( rest_is_field_included( 'id', $fields ) ) {
            $data['id'] = $post->ID;
        }

        if ( rest_is_field_included( 'date', $fields ) ) {
            

    public function get_item( $request ) {
        global $post;

        $post_id = isset( $request['post_id'] ) ? (int) $request['post_id'] : 0;

        if ( $post_id > 0 ) {
            $post = get_post( $post_id );

            // Set up postdata since this will be needed if post_id was set.             setup_postdata( $post );
        }

        $registry   = WP_Block_Type_Registry::get_instance();
        $registered = $registry->get_registered( $request['name'] );

        if ( null === $registered || ! $registered->is_dynamic() ) {
            return new WP_Error(
                'block_invalid',
                __( 'Invalid block.' ),
                array(
                    'status' => 404,
                )

function start_wp() {
    global $wp_query;

    _deprecated_function( __FUNCTION__, '1.5.0', __('new WordPress Loop') );

    // Since the old style loop is being used, advance the query iterator here.     $wp_query->next_post();

    setup_postdata( get_post() );
}

/** * Returns or prints a category ID. * * @since 0.71 * @deprecated 0.71 Use get_the_category() * @see get_the_category() * * @param bool $display Optional. Whether to display the output. Default true. * @return int Category ID. */
wp_send_json_error();
    }

    $post_id = isset( $_POST['post_ID'] ) ? (int) $_POST['post_ID'] : 0;

    if ( $post_id > 0 ) {
        $post = get_post( $post_id );

        if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) {
            wp_send_json_error();
        }
        setup_postdata( $post );
    } elseif ( ! current_user_can( 'edit_posts' ) ) { // See WP_oEmbed_Controller::get_proxy_item_permissions_check().         wp_send_json_error();
    }

    $shortcode = wp_unslash( $_POST['shortcode'] );

    preg_match( '/' . get_shortcode_regex() . '/s', $shortcode$matches );
    $atts = shortcode_parse_atts( $matches[3] );

    if ( ! empty( $matches[5] ) ) {
        $url = $matches[5];
    }

    public function single_row( $post$level = 0 ) {
        $global_post = get_post();

        $post                = get_post( $post );
        $this->current_level = $level;

        $GLOBALS['post'] = $post;
        setup_postdata( $post );

        $classes = 'iedit author-' . ( get_current_user_id() === (int) $post->post_author ? 'self' : 'other' );

        $lock_holder = wp_check_post_lock( $post->ID );

        if ( $lock_holder ) {
            $classes .= ' wp-locked';
        }

        if ( $post->post_parent ) {
            $count    = count( get_post_ancestors( $post->ID ) );
            
            /** * Fires once the loop is started. * * @since 2.0.0 * * @param WP_Query $query The WP_Query instance (passed by reference). */
            do_action_ref_array( 'loop_start', array( &$this ) );
        }

        $post = $this->next_post();
        $this->setup_postdata( $post );
    }

    /** * Determines whether there are more posts available in the loop. * * Calls the {@see 'loop_end'} action when the loop is complete. * * @since 1.5.0 * * @return bool True if posts are available, false if end of the loop. */
    

function setup_postdata( $post ) {
    global $wp_query;

    if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) {
        return $wp_query->setup_postdata( $post );
    }

    return false;
}

/** * Generates post data. * * @since 5.2.0 * * @global WP_Query $wp_query WordPress Query object. * * @param WP_Post|object|int $post WP_Post instance or Post ID/object. * @return array|false Elements of post, or false on failure. */
Home | Imprint | This part of the site doesn't use cookies.