Ejemplo n.º 1
0
/**
 * Get WordPress archive
 *
 * @param array $data Options for the function.
 * @return string|null Post title for the latest,
 * or null if none.
 *   year => months => post names
 */
function ngwp_get_archive($data)
{
    global $wpdb;
    $archive = array();
    $args = array();
    $args['numberposts'] = -1;
    if (isset($data['post_type'])) {
        $args['post_type'] = $data['post_type'];
    } else {
        $args['post_type'] = 'post';
    }
    // Get all published posts (for use later)
    $posts = get_posts($args);
    // Get years that have posts
    $years_query = "SELECT YEAR(post_date) AS year FROM " . $wpdb->prefix . "posts WHERE post_type = '" . $args['post_type'] . "' AND post_status = 'publish' GROUP BY year DESC";
    $years = $wpdb->get_results($years_query);
    // For each year, get months with posts
    foreach ($years as $year) {
        $months_query = "SELECT MONTH(post_date) AS month FROM " . $wpdb->prefix . "posts WHERE post_type = '" . $args['post_type'] . "' AND post_status = 'publish' GROUP BY month DESC";
        $months = $wpdb->get_results($months_query);
        // For each month with posts, get post titles and permalink
        foreach ($months as $key => $month) {
            $month->posts = array();
            $dateObj = DateTime::createFromFormat('!m', $month->month);
            $month->name = $dateObj->format('F');
            foreach ($posts as $post) {
                $post_timestamp = strtotime($post->post_date);
                $post_month = date('n', $post_timestamp);
                $post_year = date('Y', $post_timestamp);
                if ($post_month === $month->month && $post_year == $year->year) {
                    $post_data = array();
                    $post_data['post_title'] = $post->post_title;
                    $post_args = array();
                    $post_args['id'] = $post->ID;
                    $post_args['type'] = $post->post_type;
                    $post_args['slug'] = $post->post_name;
                    $post_data['link'] = ngwp_get_url($post_args);
                    $post_data['date_published'] = ngwp_get_published_date($post_args);
                    $month->posts[] = $post_data;
                }
            }
            if (count($month->posts) === 0) {
                unset($months[$key]);
            }
        }
        $year->months = $months;
    }
    $archive = $years;
    return $archive;
}
/**
 * Get the value of the "starship" field
 *
 * @param array $object Details of current post.
 * @param string $field_name Name of field.
 * @param WP_REST_Request $request Current request
 *
 * @return mixed
 */
function ngwp_get_adjacent_post($post_id, $previous)
{
    // Get a global post reference since get_adjacent_post() references it
    global $post;
    // Store the existing post object for later so we don't lose it
    $oldGlobal = $post;
    // Get the post object for the specified post and place it in the global variable
    $post = get_post($post_id);
    // Get the post object for the previous post
    if ($previous) {
        $adjacent_post = get_previous_post();
    } else {
        $adjacent_post = get_next_post();
    }
    // Reset our global object
    $post = $oldGlobal;
    if ('' == $adjacent_post) {
        return 0;
    }
    $adjacent_post_data = array();
    $adjacent_post_data['ID'] = $adjacent_post->ID;
    $adjacent_post_data['post_title'] = $adjacent_post->post_title;
    $adjacent_post_data['post_name'] = $adjacent_post->post_name;
    $adjacent_post_data['post_type'] = $adjacent_post->post_type;
    $object = array();
    $object['type'] = $adjacent_post_data['post_type'];
    $object['slug'] = $adjacent_post_data['post_name'];
    $adjacent_post_data['url'] = ngwp_get_url($object);
    return $adjacent_post_data;
}