0

I can't seem to find a complete way of resolving my issue. I've found parts, but it seems I'm the only one out there who has the issue I do.

I have a web working with WP, and recently I've been asked to include a blog within the web. For the authors' ease, I decided to go about doing that by creating a specific category for the blog, so all they have to do to publish in the blog is chose a category. The "blog" sections is aesthetically different, so as to give the user an idea that this is separate from the rest of the web. And obviously they need an archive, like the ones in blogs, ex.:

  • January 2018 (3)
  • February 2018 (2)
  • March 2018 (5) ...

Which I've managed by including this in the functions.php:

add_filter( 'getarchives_where', 'wse95776_archives_by_cat', 10, 2 );
/**
 * Filter the posts by category slug
 * @param $where
 * @param $r
 *
 * @return string
 */
function wse95776_archives_by_cat( $where, $r ){
    return "WHERE wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish' AND wp_terms.slug = 'BLOGCATEGORY' AND wp_term_taxonomy.taxonomy = 'category'";
}

add_filter( 'getarchives_join', 'wse95776_archives_join', 10, 2 );

/**
 * Defines the necessary joins to query the terms
 * @param $join
 * @param $r
 *
 * @return string
 */
function wse95776_archives_join( $join, $r ){
    return 'inner join wp_term_relationships on wp_posts.ID = wp_term_relationships.object_id inner join wp_term_taxonomy on wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id inner join wp_terms on wp_term_taxonomy.term_id = wp_terms.term_id';
}

Which basically limits the the posts shown in the archive to only those that are in the BLOGCATEGORY. And then I echo the archive out wherever I want it using:

echo '<ul class="blog-archive"><li>'.wp_get_archives( array( 'type' => 'monthly', 'format' => 'li', 'show_post_count' => 1, 'include'=>1 ) ).'</li></ul>';

Up to that point, everything is working perfectly, and we get the archive structure I shoed above. However, when a user clicks on one of the archive links in order to see, for example, all of the 3 posts from January 2018, they're taken into a page that gathers all posts from January 2018, i.e. the 3 that belong to the blog category but all other posts in the website, no matter the category.

So basically my question is how do I obtain a completely functioning separate archive for posts for only one or two specific categories?

  • You could either go down an easier path - create different custom post types so they will automatically have their own separate archives - or create custom pseudo-archives by creating a Page for each one, then creating a Page Template with pre_get_posts filters to restrict the posts to the blog category. – WebElaine Apr 19 '18 at 13:31
  • Thanks for your answer, @WebElaine! Because of other functions of this website (including the use of PolyLang to organise 4 different languages) it'd be more complex to create custom post types and implement them, so I'd be interested in creating pseudo-archives for each of the 2 categories I need. Is there any resource you can recommend where I can find complete info on that in order to know where to start? Thanks in advance! – QuestionerNo27 Apr 19 '18 at 16:03
  • Hi, again, @WebElaine, in the end I decided to look for a plugin that would facilitate the creation of new post types and help me tie in all the services I have working in the website. As I supposed, it wasn't as easy as if I had no other complications set up, but in the end I got it to work... But, as always, now I run into another issue resulting from it. As I mentioned, I'm running PolyLang; the default language is English, so no issues there, but the archive links to other languages have the language part of the url placed after the CPT, thereby breaking the workable url... any ideas? – QuestionerNo27 Apr 19 '18 at 18:25
  • I would suggest asking the authors of PolyLang since they're familiar with the way their plugin works. I'm afraid I don't have experience with it. – WebElaine Apr 19 '18 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.