WordPress.org

Ready to get started?Download WordPress

Forums

wp_get_archives and Conditional Tags (18 posts)

  1. Buckycat7
    Member
    Posted 4 years ago #

    I have two quick questions. But here is how WP is being used, we are using it more in a CMS application, with a section for company news and press releases along with a blog. The post page is set to the blog and I've removed the news category from showing in in both the feed and blog post page. However I'm trying to add the wp_get_archives tag and can't figure out how to remove the news category from showing.

    This is what I was trying:

    <ul>
    <?php query_posts("cat=-1"); ?>
    <?php wp_get_archives('type=postbypost&limit=5'); ?>
    </ul>

    Clearly this doesn't work, so want can I do to exclude cat. 1 from the archives?

    Secondly, I'm using a CSS sprite Menu and using conditional tags to make the sprite turn to an active state when a visitor is on the page or a child page. However the code I am use will not turn the blog menu item to active when someone is one the blog page. Again the blog page is set to the post page in the reading setting in the WP-Admin Dashboard.

    Here is that line of code:
    <li id="blog"><a <?php if ( is_page('blog') || $post->post_parent == '95' ) { echo " class=\"active\""; } ?> href="blog">Blog</a></li>

    Any reason way it just won't work on that one page?

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 4 years ago #

    You can't do that with the wp_get_archives function. It doesn't work that way.

    To do what you want you'll need to use a couple filters and modify the SQL queries it's doing. Something sorta like this:

    function customarchives_join($x) {
    	return $x." INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
    }
    add_filter('getarchives_join','customarchives_join');
    
    function customarchives_where($x) {
    	$exclude='1'; // cat id to exclude
    	return $x." AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id NOT IN ($exclude)";
    }
    add_filter('getarchives_where','customarchives_where');
  3. Buckycat7
    Member
    Posted 4 years ago #

    Okay so would I place that in the functions file and then call <?php getartchives_where(); ?> ?

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 4 years ago #

    No, you would place that (or something like it, I haven't tested that) in the functions file and then call wp_get_archives() as per normal.

  5. Buckycat7
    Member
    Posted 4 years ago #

    I'm not that great at PHP, I understand the basics and can code most of a WordPress theme, however I placed that code into my functions file and now no post are showing up. Could I get a little help debugging it?

  6. Buckycat7
    Member
    Posted 4 years ago #

    I have the archives problem figured out.

    Any thoughts on what the Conditional Tag won't work with the blog page?

  7. Lumpysimon
    Member
    Posted 4 years ago #

    The solution given by Otto42 above requres a small change in order to work: each function needs to access the global wpdb variable. Here's the amended code:

    add_filter( 'getarchives_where', 'customarchives_where' );
    add_filter( 'getarchives_join', 'customarchives_join' );
    
    function customarchives_join( $x ) {
    
    	global $wpdb;
    
    	return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
    
    }
    
    function customarchives_where( $x ) {
    
    	global $wpdb;
    
    	$exclude = '1'; // category id to exclude
    
    	return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id NOT IN ($exclude)";
    
    }
  8. schoe
    Member
    Posted 4 years ago #

    Thank you Otto42, thank you lumpysimon. That's exactly what I was looking for. Works much better than my tries excluding a bunch of posts by IDs with the "getarchives_where" filter only...
    And you can even exclude more than one category:

    $exclude = '1,5'; // category ids to exclude

    brilliant!

  9. karl19
    Member
    Posted 4 years ago #

    Great, this was exactly what we'd been looking for as well - thanks for sharing!

  10. chavo
    Member
    Posted 4 years ago #

    Hi. This is exactly what I was looking for. I want to know if is possible to use conditional if statements with this function. I will clarify what I want to do:

    I have a parent category named as "News" with 2 child categories "esp" (for spanish posts) and "eng" (for english post). So...I'm managing what type of posts to show from "News" category depending of which is the current language SESSION. If session = esp, I display (with a query_post call) only post from "esp" child category, else if session = eng, I display posts from "eng" child category. What I want to know is if is possible to use the excluding category function as this way:

    if session = esp, so exclude "eng" post
    else if session = eng, exclude "esp" post.

    That's the first thing that I want to attemp. Once I could resolve this, I have to research how to show wp_get_archives in english months format por "eng" and in spanish month format for "esp".

    Thanks in advance!

    ps: sorry for my bad english.

  11. juliageek
    Member
    Posted 4 years ago #

    Hi
    I can't get this to work - am I right in putting it into wp-includes/functions.php? Is there a correct place to put it?
    Thanks

  12. schoe
    Member
    Posted 4 years ago #

    Hi juliageek,

    the trick with changing the behavior of wordpress is, that you never change any file of your WP installation in the wp-admin or the wp-includes folder.

    All changes are made in wp-content

    • either in your theme: wp-content/themes/my_theme/functions.php
    • or in a plugin you have installed: wp-content/plugins/my_plugin/my_functions.php
  13. SCRWD
    Member
    Posted 4 years ago #

    Hi,

    I'm looking to expand this slightly but I'm not sure how to do it.

    Basically I'd like to to pass the categories to include rather than exclude, also i'd like to be able to get it to show archives for 'future' posts as well (as I am trying to use it for managing events within a custom template - sidebar-gigs.php)

    Somehing like:

    wp_get_archives('type=monthly&category=11,12&post_status=published,future');

    Can anyone help?

  14. doriggidy
    Member
    Posted 3 years ago #

    I found this thread and I am also interested in including instead of exluding

  15. paul@hygge.ie
    Member
    Posted 3 years ago #

    Hi,

    Thankyou, Thankyou, Thankyou for this..its superb

    The following code will only display a specific category in the archive list.

    add_filter( 'getarchives_where', 'customarchives_where' );
    add_filter( 'getarchives_join', 'customarchives_join' );
    
    function customarchives_join( $x ) {
    
    	global $wpdb;
    
    	return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
    
    }
    
    function customarchives_where( $x ) {
    
    	global $wpdb;
    
    	//$exclude = '1'; // category id to exclude
    	//return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id NOT IN ($exclude)";
    	$include = '1'; // category id to include
    	return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id IN ($include)";
    
    }
  16. nickmac
    Member
    Posted 3 years ago #

    Can someone please explain the exact usage/implementation of this.

    thank you!

  17. petrusfajarsubekti
    Member
    Posted 3 years ago #

    Yes, i still don't understand the benefits of this thing.
    If it can make any good, maybe i will implement it on my blog
    Thank you before

  18. Tammy Hart
    Member
    Posted 3 years ago #

    Thank you, thank you, thank you!

Topic Closed

This topic has been closed to new replies.

About this Topic