• Resolved Doodlebee

    (@doodlebee)


    I’m working on a site where I need to use the category name/description. On the home page, I need to display the category name and description of only the newest category created. I have this working as it should – except the information won’t display *unless* there’s a post within the category. I need the information to display pretty much as soon as the category is created, whether there’s a post in it or not.

    My (working) code looks like this:

    <?php // custom query to show Newest Category
      $new_query = new WP_Query('category_name=new_category&showposts=1');
      while ($new_query->have_posts()) : $new_query->the_post();
      ?>
    
      <?php
        global $wpdb;
        $newcat = $wpdb->get_var("SELECT term_id FROM $wpdb->terms WHERE term_id > 0 ORDER BY term_id DESC LIMIT 1");
        query_posts("cat=$newcat");
      ?>
    
    // stuff to display the category name and description here

    Like I said, the above works *great* – but only if the newest category created has a post in it. Would anyone know how to fix that so if there’s a new category, it’ll show even though there’s no posts associated with it?

    Thanks πŸ™‚

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter Doodlebee

    (@doodlebee)

    Phooey. The above isn’t working *totally* right, either. I also need – if any posts are made, only the newest made *category* displays on the front page. Right now, if a post is made in an old category, then it’ll bring the old category to the front (because of the new post) – I can’t have that either.

    I need the newest category to stay on the home page, regardless of whether or not there’s any posts in the category, and the newest category has to be on the front page, even if a post is made in an older category.

    Urgh…am I asking too much? I hope not.

    Urgh…am I asking too much?
    Probably.
    I don’t think categories have anything “date” related in their DB table – so I don’t see how can you select ‘the newest’.

    Thread Starter Doodlebee

    (@doodlebee)

    well, I got it, partially. I fixed the code above so that I *can* pull the newest category ID (which, in 2.3, is the terms_id and the taxonomy of “category”). So *that* part is working – I can pull the ID of the newest category, no problem.

    However, when I create a post, it seems to override the category. I’ve tried doing a query_posts('cat=22'); (“22” is currently the newest cat ID), but it doesn’t matter – if I make a new post in an older category, the query gets overridden.

    Right now, my (getting there) code looks like so – this is the code that *will* pull out the most recently created/newest category, but the problems above remain:

    <?php // custom query for main category
      $new_query = new WP_Query('category_name=new_category&showposts=1');
      while ($new_query->have_posts()) : $new_query->the_post();
    
    // get the most recently created category
        global $wpdb;
        $newcat = $wpdb->get_var("SELECT term_id, taxonomy
                                  FROM $wpdb->term_taxonomy
                                  WHERE term_id > 0
                                  AND taxonomy = 'category'
                                  ORDER BY term_id
                                  DESC
                                  LIMIT 1
                                 ");
        $newcat_query = "'cat=" . $newcat . "'";
        query_posts($newcat_query);
      ?>
          <div class="post_current" id="post-<?php the_ID(); ?>">
    				<h1>Current:</h1>
    
    				<div class="entry">
    					<?php
              $cat = get_the_category();
              $cat = $cat[0];
              echo '<h2><a href="' . get_category_link($cat) . '">' . $cat->cat_name . '</a></h2>';
              echo '<p>' . $cat->category_description . '</p>';
              ?>
    				</div>

    So would anyone know why the query is getting overridden? It seems to me that, if I’m querying *only* the newest category ID (which, when I print/echo out the $newcat_query, it *is* correct), it should only look for posts in the ID specified – but if a newer post is made, then the query gets overridden, and the newest post pulls the older category in.

    Hope I’m making sense.

    You should try the wp-pro list… that’s what we always tell to those that do paid jobs and seek help here.

    Thread Starter Doodlebee

    (@doodlebee)

    That’s true – I direct people that way, as well. But this isn’t a paid job – which is why I was looking over here πŸ™‚ (You know what they say about the word “assume”… πŸ˜‰ )

    After your query_posts($newcat_query); wouldn’t you have another loop like:

    <?php while (have_posts()) : the_post(); ?>
        <!-- Do special_cat stuff... -->
      <?php endwhile;?>
    Thread Starter Doodlebee

    (@doodlebee)

    Michael – although normally you would be right – that wasn’t working for me. I’m going to try to keep this post small – so I’m not going to bother to explain why. But your suggestion gave me an idea – which I’m happy to say *did* work. (Turns out my issue was that I was upgin the “$wpdb->get_vars” query – which only returns *one* variable. I needed access to several.

    So, in case anyone in the future needs this, I’m going to post my solution here. It’s ugly as hell, but it works, so I don’t care πŸ˜‰

    What I was trying to do was have – on the home page – the newest category display, regardless of whether or not there were posts under that category. If there *were*, then the most recent post would display beneath the category.

    My solution was this:

    <?php // get the most recently created category
      global $wpdb;
      $catquery = ("SELECT $wpdb->term_taxonomy.term_id, $wpdb->term_taxonomy.taxonomy, $wpdb->term_taxonomy.description, $wpdb->term_taxonomy.count, $wpdb->terms.term_id, $wpdb->terms.name, $wpdb->terms.slug
                    FROM $wpdb->term_taxonomy, $wpdb->terms
                    WHERE $wpdb->term_taxonomy.term_id > 0
                    AND $wpdb->term_taxonomy.taxonomy = 'category'
                    AND $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id
                    ORDER BY $wpdb->terms.term_id
                    DESC
                    LIMIT 1
                    ");
    
        $newcat = $wpdb->get_row($catquery, ARRAY_A);
        $newcatid = $newcat['term_id'];
        $newcatname = $newcat['name'];
        $newcatdesc = $newcat['description'];
        $newcatslug = $newcat['slug'];
        $newcatcount = $newcat['count'];
     ?>
    
    //display stuff here
    
    <?php // if the category had posts associate with it, show the most recent one
        $newcat_query = new WP_Query('category_name=' . $newcatslug . '&showposts=1');
        while ($newcat_query->have_posts()) : $newcat_query->the_post(); ?>
    
    //display stuff here		
    
        <?php endwhile;?>

    And now I have my prettiness. Hope it helps someone else in the future πŸ™‚

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Display category with no posts?’ is closed to new replies.