• Hi,

    I’d like to be able to select what archived posts will be displayed (by category, …)

    I tried to use the kwebble plugin. But it broke my site and I had to uninstall it.

    I used some code in functions.php (as indicated at the end of comments). But if it was successfully filtering archives, the URLs the archive links lead to a page where all the categories archives were displayed…
    🙁

    Any help for that ?
    Paul

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter paulsellis

    (@paulsellis)

    I haven’t found any solution for that…

    That’s not possible ?

    Moderator keesiemeijer

    (@keesiemeijer)

    Is this for monthly archives?
    see: http://wordpress.org/support/topic/remove-cetain-post-categories-from-blog-sidebar-archive-list

    Try it with this in your theme’s functions.php:

    define("INCLUDED_CATEGORIES", '25,55,57');
    
    add_filter( 'getarchives_join' , 'getarchives_join_filter');
    function getarchives_join_filter( $join ) {
      global $wpdb;
      return $join . " INNER JOIN {$wpdb->term_relationships} tr ON ($wpdb->posts.ID = tr.object_id) INNER JOIN {$wpdb->term_taxonomy} tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)";
    }
    
    add_filter( 'getarchives_where' , 'getarchives_where_filter');
    function getarchives_where_filter( $where ) {
      global $wpdb;
    
      $include = INCLUDED_CATEGORIES; // category ids to include
      return $where . " AND tt.taxonomy = 'category' AND tt.term_id IN ($include)";
    
      }
    
    // exclude categories on monthly archive pages
    function my_post_queries( $query ) {
      // do not alter the query on wp-admin pages and only alter it if it's the main query
      if (!is_admin() && $query->is_main_query()){
    
        // alter the query for monthly archive pages
        if(is_archive() && is_month()){
          $query->set('category__in', array(INCLUDED_CATEGORIES));
        }
      }
    }
    
    add_action( 'pre_get_posts', 'my_post_queries' );

    Change the category id’s in define("INCLUDED_CATEGORIES", '25,55,57'); to the IDs you want to include.

    With this in your functions.php wp_get_archives() will be filtered by the included categories.
    http://codex.wordpress.org/Function_Reference/wp_get_archives

    Thread Starter paulsellis

    (@paulsellis)

    Thanks for your help keesiemeijer

    Unfortunately it works incompletely:

    The months archives are well filtered
    – if a month has only posts from excluded categories, it will not be displayed in the archive months list. Right it works
    – if a month has posts from both included and excluded categories, if the month is displayed : OK that’s normal

    But in the last case, if we click on the month link, then all the posts from this month are displayed

    I haven’t seen any fix for that…

    Moderator keesiemeijer

    (@keesiemeijer)

    Does your theme’s archive.php query the loop?

    Can you paste and submit the full code of archive.php of your theme into a pastebin.com and post the link to it here? see the Forum Rules for posting code and using a pastebin.

    Thread Starter paulsellis

    (@paulsellis)

    Thank you for your answer

    I don’t have an archive.php file in my theme.
    I saw that the archives list (annual, month, week) was ruled by index.php.
    Here is the code fron paste bin.com :
    http://pastebin.com/ZckNQjuG

    I have no idea if it queries the loop…

    Moderator keesiemeijer

    (@keesiemeijer)

    Not sure why it’s not working but try changing this:

    $query->set('category__in', array(INCLUDED_CATEGORIES));

    to this:

    $include = array_map('trim', explode(',', (string) INCLUDED_CATEGORIES));
    $query->set('category__in', $include);

    Thread Starter paulsellis

    (@paulsellis)

    keesiemeijer, I apologize

    The first code you gave to me was OK
    I don’t know what confusion I made when I tested it, but it works perfectly.

    Thanks a lot and sorry again.

    Paul

    Moderator keesiemeijer

    (@keesiemeijer)

    No problem. I’m glad you got it resolved 🙂

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Displaying categorized archives ?’ is closed to new replies.