WordPress.org

Ready to get started?Download WordPress

Forums

Displaying categorized archives ? (9 posts)

  1. PaulSellis
    Member
    Posted 1 year ago #

    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

  2. PaulSellis
    Member
    Posted 1 year ago #

    I haven't found any solution for that...

    That's not possible ?

  3. keesiemeijer
    moderator
    Posted 1 year ago #

    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

  4. PaulSellis
    Member
    Posted 1 year ago #

    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...

  5. keesiemeijer
    moderator
    Posted 1 year ago #

    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.

  6. PaulSellis
    Member
    Posted 1 year ago #

    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...

  7. keesiemeijer
    moderator
    Posted 1 year ago #

    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);
  8. PaulSellis
    Member
    Posted 1 year ago #

    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

  9. keesiemeijer
    moderator
    Posted 1 year ago #

    No problem. I'm glad you got it resolved :-)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.