admin管理员组

文章数量:1277265

Archive per year of a custom post getting the wrong post

I got the wp_get_the_archives() to list the years of my post but even though the url is ok, the post shown in the year archive just give the last post depending on how many post I have per year.

For example, I have 3 post for 2008, the archive of that year will only show me 3 posts but just the last three (2 from 2021 and 1 from 2016). If I have 1 from 2016, the archive will show me one post, but is just the last one from 2021. And so on... what did I do wrong?

My functions.php file


<?php 
function recursos_ah6studio(){
    wp_enqueue_style('bootstrap', get_theme_file_uri('/css/bootstrap.min.css'));
    wp_enqueue_style('slick', get_theme_file_uri('/slick/slick.css'));
    wp_enqueue_style('slick-theme', get_theme_file_uri('/slick/slick-theme.css'));
    wp_enqueue_style('magnific-popup', get_theme_file_uri('/magnific-popup/magnific-popup.css'));
    wp_enqueue_style('font-awesome', get_theme_file_uri('/fontawesome-5.5/css/all.min.css'));
    wp_enqueue_style('estilo_ah6studio', get_stylesheet_uri());
    
    /*wp_enqueue_script('mainJS', get_theme_file_uri('/js/main_js.js'), NULL, '1.0', true); */
    wp_enqueue_script('jquery', get_theme_file_uri('/js/jquery-3.5.1.min.js'), NULL, '1.0', true);
    wp_enqueue_script('popper', get_theme_file_uri('/js/popper.min.js'), NULL, '1.0', true);
    wp_enqueue_script('boostrap', get_theme_file_uri('/js/bootstrap.min.js'), NULL, '1.0', true);
    wp_enqueue_script('slickJS', get_theme_file_uri('/slick/slick.min.js'), NULL, '1.0', true);
    wp_enqueue_script('magnific-popup', get_theme_file_uri('/magnific-popup/jquery.magnific-popup.min.js'), NULL, '1.0', true);
    wp_enqueue_script('easingJS', get_theme_file_uri('/js/easing.min.js'), NULL, '1.0', true);
    wp_enqueue_script('singlePageJS', get_theme_file_uri('/js/jquery.singlePageNav.min.js'), NULL, '1.0', true); 
    wp_enqueue_script('tool-tip', get_theme_file_uri('/js/tooltip.js'), NULL, '1.0', true);

} 
add_action('wp_enqueue_scripts', 'recursos_ah6studio');

function mytheme_enqueue_front_page_scripts() {
    if( is_front_page() )
    {
        wp_enqueue_script('bkcground-random', get_theme_file_uri('/js/background_random.js'), NULL, '1.0', true);   
    }
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_front_page_scripts' );

function ourWidgetInit(){

register_sidebar( array (
'name' => 'Sidebar',
'id' => 'sidebar1',

));

register_sidebar( array (
    'name' => 'Desplegable',
    'id' => 'desplega1',
    'before_widget'=>'<div class="widget-item">',
    'after_widget'=> '</div>',
    'before_title'=>'<h6 class="category_title">',
    'after_title'=>'</h6>', 
    ));
}

add_action('widgets_init', 'ourWidgetInit');

//Añadir el thumbnail//
function ah6studio_setup() {
    add_theme_support('post-thumbnails');
    add_theme_support('title-tag'); 
}
add_action('after_setup_theme','ah6studio_setup');

My plugin file ah6_postypes.php


<?php

function ah6_papers() {
    register_post_type('papers', array(
        'has_archive'=> true,
        'public'=> true,
        'posts_per_page' => -1,
        'labels'=> array(
            'name'=>'Papers',
            'add_new'=> 'Agregar un Paper',
            'edit_item'=>'Editar un Paper',
            'all_items'=>'Todos los Papers',
            'singular_name'=>'Paper'
            
        ),
        'taxonomies' => array('temas'),
        'menu_icon'=>  'dashicons-media-text',
    ));
}

add_action('init', 'ah6_papers');


function papers_categories() {
    //Taxonomía: TEMAS
    //Traducción para el backend

      $labels = array(
        'name' => _x( 'Temas', 'taxonomy general name' ),
        'singular_name' => _( 'Tema', 'taxonomy singular name' ),
        'search_items' =>  __( 'Buscar tema' ),
        'all_items' => __( 'Todos los temas' ),
        'parent_item' => __( 'Tema padre' ),
        'parent_item_colon' => __( 'Tema padre:' ),
        'edit_item' => __( 'Editar tema' ), 
        'update_item' => __( 'Actualizar tema' ),
        'add_new_item' => __( 'Añadir nuevo tema' ),
        'new_item_name' => __( 'Nombre del nuevo tema' ),
        'menu_name' => __( 'Temas' ),
      );    
     
    // Registrando la taxonomía
      register_taxonomy('temas',array('papers'), array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_in_rest' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'temas' ),
      ));

      //Taxonomía: TERRITORIOS
    //Traducción para el backend

    $labels = array(
        'name' => _x( 'Territorios', 'taxonomy general name' ),
        'singular_name' => _( 'Territorio', 'taxonomy singular name' ),
        'search_items' =>  __( 'Buscar territorio' ),
        'all_items' => __( 'Todos los territorios' ),
        'parent_item' => __( 'Territorio padre' ),
        'parent_item_colon' => __( 'Territorio padre:' ),
        'edit_item' => __( 'Editar territorio' ), 
        'update_item' => __( 'Actualizar territorio' ),
        'add_new_item' => __( 'Añadir nuevo territorio' ),
        'new_item_name' => __( 'Nombre del nuevo territorio' ),
        'menu_name' => __( 'Territorios' ),
      );    
     
    // Registrando la taxonomía
      register_taxonomy('territorios',array('papers'), array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_in_rest' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'territorios' ),
      ));
     
    }

    add_action( 'init', 'papers_categories', 0 );

Archive per year of a custom post getting the wrong post

I got the wp_get_the_archives() to list the years of my post but even though the url is ok, the post shown in the year archive just give the last post depending on how many post I have per year.

For example, I have 3 post for 2008, the archive of that year will only show me 3 posts but just the last three (2 from 2021 and 1 from 2016). If I have 1 from 2016, the archive will show me one post, but is just the last one from 2021. And so on... what did I do wrong?

My functions.php file


<?php 
function recursos_ah6studio(){
    wp_enqueue_style('bootstrap', get_theme_file_uri('/css/bootstrap.min.css'));
    wp_enqueue_style('slick', get_theme_file_uri('/slick/slick.css'));
    wp_enqueue_style('slick-theme', get_theme_file_uri('/slick/slick-theme.css'));
    wp_enqueue_style('magnific-popup', get_theme_file_uri('/magnific-popup/magnific-popup.css'));
    wp_enqueue_style('font-awesome', get_theme_file_uri('/fontawesome-5.5/css/all.min.css'));
    wp_enqueue_style('estilo_ah6studio', get_stylesheet_uri());
    
    /*wp_enqueue_script('mainJS', get_theme_file_uri('/js/main_js.js'), NULL, '1.0', true); */
    wp_enqueue_script('jquery', get_theme_file_uri('/js/jquery-3.5.1.min.js'), NULL, '1.0', true);
    wp_enqueue_script('popper', get_theme_file_uri('/js/popper.min.js'), NULL, '1.0', true);
    wp_enqueue_script('boostrap', get_theme_file_uri('/js/bootstrap.min.js'), NULL, '1.0', true);
    wp_enqueue_script('slickJS', get_theme_file_uri('/slick/slick.min.js'), NULL, '1.0', true);
    wp_enqueue_script('magnific-popup', get_theme_file_uri('/magnific-popup/jquery.magnific-popup.min.js'), NULL, '1.0', true);
    wp_enqueue_script('easingJS', get_theme_file_uri('/js/easing.min.js'), NULL, '1.0', true);
    wp_enqueue_script('singlePageJS', get_theme_file_uri('/js/jquery.singlePageNav.min.js'), NULL, '1.0', true); 
    wp_enqueue_script('tool-tip', get_theme_file_uri('/js/tooltip.js'), NULL, '1.0', true);

} 
add_action('wp_enqueue_scripts', 'recursos_ah6studio');

function mytheme_enqueue_front_page_scripts() {
    if( is_front_page() )
    {
        wp_enqueue_script('bkcground-random', get_theme_file_uri('/js/background_random.js'), NULL, '1.0', true);   
    }
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_front_page_scripts' );

function ourWidgetInit(){

register_sidebar( array (
'name' => 'Sidebar',
'id' => 'sidebar1',

));

register_sidebar( array (
    'name' => 'Desplegable',
    'id' => 'desplega1',
    'before_widget'=>'<div class="widget-item">',
    'after_widget'=> '</div>',
    'before_title'=>'<h6 class="category_title">',
    'after_title'=>'</h6>', 
    ));
}

add_action('widgets_init', 'ourWidgetInit');

//Añadir el thumbnail//
function ah6studio_setup() {
    add_theme_support('post-thumbnails');
    add_theme_support('title-tag'); 
}
add_action('after_setup_theme','ah6studio_setup');

My plugin file ah6_postypes.php


<?php

function ah6_papers() {
    register_post_type('papers', array(
        'has_archive'=> true,
        'public'=> true,
        'posts_per_page' => -1,
        'labels'=> array(
            'name'=>'Papers',
            'add_new'=> 'Agregar un Paper',
            'edit_item'=>'Editar un Paper',
            'all_items'=>'Todos los Papers',
            'singular_name'=>'Paper'
            
        ),
        'taxonomies' => array('temas'),
        'menu_icon'=>  'dashicons-media-text',
    ));
}

add_action('init', 'ah6_papers');


function papers_categories() {
    //Taxonomía: TEMAS
    //Traducción para el backend

      $labels = array(
        'name' => _x( 'Temas', 'taxonomy general name' ),
        'singular_name' => _( 'Tema', 'taxonomy singular name' ),
        'search_items' =>  __( 'Buscar tema' ),
        'all_items' => __( 'Todos los temas' ),
        'parent_item' => __( 'Tema padre' ),
        'parent_item_colon' => __( 'Tema padre:' ),
        'edit_item' => __( 'Editar tema' ), 
        'update_item' => __( 'Actualizar tema' ),
        'add_new_item' => __( 'Añadir nuevo tema' ),
        'new_item_name' => __( 'Nombre del nuevo tema' ),
        'menu_name' => __( 'Temas' ),
      );    
     
    // Registrando la taxonomía
      register_taxonomy('temas',array('papers'), array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_in_rest' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'temas' ),
      ));

      //Taxonomía: TERRITORIOS
    //Traducción para el backend

    $labels = array(
        'name' => _x( 'Territorios', 'taxonomy general name' ),
        'singular_name' => _( 'Territorio', 'taxonomy singular name' ),
        'search_items' =>  __( 'Buscar territorio' ),
        'all_items' => __( 'Todos los territorios' ),
        'parent_item' => __( 'Territorio padre' ),
        'parent_item_colon' => __( 'Territorio padre:' ),
        'edit_item' => __( 'Editar territorio' ), 
        'update_item' => __( 'Actualizar territorio' ),
        'add_new_item' => __( 'Añadir nuevo territorio' ),
        'new_item_name' => __( 'Nombre del nuevo territorio' ),
        'menu_name' => __( 'Territorios' ),
      );    
     
    // Registrando la taxonomía
      register_taxonomy('territorios',array('papers'), array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_in_rest' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'territorios' ),
      ));
     
    }

    add_action( 'init', 'papers_categories', 0 );

Share Improve this question asked Oct 3, 2021 at 22:57 Daniel LeandroDaniel Leandro 175 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Try It You need is make a filter for wp_get_archives();

function custom_post_type_archive_yearly($where,$args){  
    $post_type  = isset($args['post_type'])  ? $args['post_type']  : 'post';  
    $where = "WHERE post_type = '$post_type' AND post_status = 'publish'";
    return $where;  
}

Call this filter Hook:

add_filter( 'getarchives_where','custom_post_type_archive_yearly',10,3);

New you can Disply your Custom Post:

$args = array(
    'post_type'    => 'your_custom_post_type',
    'type'         => 'monthly',
    'echo'         => 0
);
var_dump(wp_get_archives($args));

本文标签: Archive per year of a custom post getting the wrong post