WordPress.org

Ready to get started?Download WordPress

Forums

List post titles by category? (34 posts)

  1. lokjah
    Member
    Posted 8 years ago #

    I would like to be able to show a list of posts (basically an archive) by category. I would like to run sections of my site with this method.

    ex- you click on the music section of the site and it brings you to a simple index file that lists all the posts in the music category.

    I cant seem to find anything along those lines from the codex on archives or categories, and havent found a plugin...

    something like what goes on here:
    http://codex.wordpress.org/Template_Tags/list_cats

    but in "plain speak" mine would be to list posts by category.

    or something like:
    http://codex.wordpress.org/Template_Tags/get_archives

    plain speak- get archives post by post, BUT with a single category

    any help would be appreciated...

    tia
    løk

  2. Joshua Sigar
    Member
    Posted 8 years ago #

  3. lokjah
    Member
    Posted 8 years ago #

    <?php $my_query = new WP_Query('category_name=special_cat&showposts=10'); ?>

    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    // Do stuff.
    <?php endwhile; ?>

    this one? if so, im not quite sure how to use it. do I just need this loop by itself?

    and "category_name" how would I use the category ID?

    thanks alphaiode

  4. lokjah
    Member
    Posted 8 years ago #

    ok ive got something like this so far, am I doing it right?

    <?php $my_query = new WP_Query('category_id=1&showposts=10'); ?>

    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <h2 id="post-<?php the_ID(); ?>">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <?php the_title(); ?></a></h2>
    <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
    <?php endwhile; ?>

  5. lokjah
    Member
    Posted 8 years ago #

    this seems to be working, although it doesnt just list category_id=1 posts, it lists others as well.....

  6. Joshua Sigar
    Member
    Posted 8 years ago #

    If you want to pass category id, use keyword cat
    If you want to pass category name, use keyword category_name
    There is no category_id

  7. lokjah
    Member
    Posted 8 years ago #

    cat=1
    returns an error:

    WordPress database error: [Not unique table/alias: 'mainwp_post2cat']
    SELECT DISTINCT * FROM mainwp_posts LEFT JOIN mainwp_post2cat ON (mainwp_posts.ID = mainwp_post2cat.post_id) LEFT JOIN mainwp_post2cat ON (mainwp_posts.ID = mainwp_post2cat.post_id) WHERE 1=1 AND (category_id = 1) AND post_date_gmt <= '2005-05-05 02:02:59' AND (post_status = "publish") AND (category_id = 1 OR category_id = 5 OR category_id = 6) GROUP BY mainwp_posts.ID ORDER BY post_date DESC LIMIT 0, 999

  8. lokjah
    Member
    Posted 8 years ago #

    also interesting is that bit:

    AND (category_id = 1 OR category_id = 5 OR category_id = 6)

    those are the very categories that were showing up in addition to category 1

  9. Joshua Sigar
    Member
    Posted 8 years ago #

    I'm lost right now. Is passing category_name working for you?

  10. lokjah
    Member
    Posted 8 years ago #

    no that throws the same error as using "cat"

    the one that works sort of is the one i posted above:

    <?php $my_query = new WP_Query('category_id=1&showposts=10'); ?>

    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <h2 id="post-<?php the_ID(); ?>">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <?php the_title(); ?></a></h2>
    <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
    <?php endwhile; ?>

    that works but doesnt show just category 1 posts, it shows 1, 5 and 6 like the error message states above.. so that is coming somewhere from the original example you pointed me to

  11. Joshua Sigar
    Member
    Posted 8 years ago #

    Do, instead, example 2 in that CODEX link. In this example, the regular loop will be overwritten, so make sure you call the custom loop after the regular loop (if any).

  12. lokjah
    Member
    Posted 8 years ago #

    ok that one works!

    now where its showposts=10

    is there a way to substitute the # (10 in this case) with something like "all" or will I need to use some exorbidant # like 999 ?

  13. Joshua Sigar
    Member
    Posted 8 years ago #

    You could pass posts_per_page=-1
    Don't forget the ampersand symbol (&) between parameters.

  14. lokjah
    Member
    Posted 8 years ago #

    sweet, last thing is there anyway that I can add multiple categories? I have a news link that i want to show posts from a few categories...

    <?php query_posts('cat=1,4,5&showposts=3'); ?>

    is that written right?

  15. Joshua Sigar
    Member
    Posted 8 years ago #

    separated by a comma or a whitespace, I believe

  16. lokjah
    Member
    Posted 8 years ago #

    thanks for all the help alphaoide :-)

  17. greatbananas
    Member
    Posted 8 years ago #

    hi, I've been reading this and haven't had luck making things work. What exactly did you use?

  18. greatbananas
    Member
    Posted 8 years ago #

    ok just to clarify a little bit. I tried using the follow code and it works in just putting out the most recent posts:

    <?php $my_query = new WP_Query('category_id=1&showposts=10'); ?>

    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <h2 id="post-<?php the_ID(); ?>">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <?php the_title(); ?></a></h2>
    <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
    <?php endwhile; ?>

    However, I want it to spit out the most recent posts from the current category.

    Alphaoide, when you said do example 2 from CODEX, I am a little lost and what I am exactly suppose to do.

  19. Kafkaesqui

    Posted 8 years ago #

    It should be:

    <?php $my_query = new WP_Query('cat=1&showposts=10'); ?>

    Don't know where category_id comes from, but I don't recognize it as a valid WP query var.

    "I want it to spit out the most recent posts from the current category."

    If you mean when displaying any category:

    <?php $my_query = new WP_Query("cat=$cat&showposts=10"); ?>

    $cat is a global variable for category queries. Note the double-quotes used to enclose the parameters in this case.

  20. greatbananas
    Member
    Posted 8 years ago #

    Ok, I found out why it isn't working for me.

    <?php $my_query = new WP_Query("cat=$cat&showposts=10"); ?>

    This code works perfectly well when I am adding it in the archive.php.

    The thing is I want it to be used right below the actual post which is in single.php. So this is what I used:

    <?php foreach((get_the_category()) as $cat) { ?>
    <p><?php $my_query = new WP_Query("cat=$cat->cat_ID&showposts=10"); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <h2 id="post-<?php the_ID(); ?>">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <?php the_title(); ?></a></h2>
    <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
    <?php endwhile; ?> </p>
    <?php } ?>

    This works. When I am viewing a post, it creates a list of posts that are in the same category of the post I am viewing. The only problem is that it also lists the post I am viewing in the list. Is there a way to skip the post I am reading?

  21. Kafkaesqui

    Posted 8 years ago #

    Difficult to work this along with WP_Query. If you want exactly 10 posts, you first need to query 11; from this you can test on say the post IDs (you'll want to collect it for the current post's *before* the custom query), and either pass over the current post if in your query object, or discard the 11th once you have 10 displayed.

    Another method would be to set up your own SQL query for the posts in the category so you can avoid the current post entirely. Try swapping out the $my_query = new and while lines with the following:

    <p><?php $my_query = $wpdb->get_results("SELECT $wpdb->posts.* FROM $wpdb->posts, $wpdb->post2cat WHERE category_id = '$cat->cat_ID' AND ID = post_id AND post_id <> '$id' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 10"); ?>
    <?php while(list(,$post) = each($my_query)) : ?>

    It looks complicated (actually, it is complicated), but it should do what you're after without any additional changes. This version should also incur less overhead.

  22. Kafkaesqui

    Posted 8 years ago #

    Almost forgot to mention that if the above doesn't work, slip this in just above the new $my_query line:

    <?php global $post; $id = $post->ID; ?>

  23. greatbananas
    Member
    Posted 8 years ago #

    Your code kind of works. If I add the following code before your code, it works fine:
    <?php foreach((get_the_category()) as $cat) {
    $cat->cat_ID;
    } ?>

    If I add the following or not:
    <?php global $post; $id = $post->ID; ?>

    I get this error:
    "Warning: Variable passed to each() is not an array or object in /home/happy/public_html/wp-content/themes/john-3-col/single.php on line 24"

    Kafkaesqui, thanks for your help. :) Let me know if you see any problems with this and if not, I'll post my final code so others can use it.

  24. Kafkaesqui

    Posted 8 years ago #

    This is the exact code I tested before replying above:

    http://guff.szub.net/wp-content/sourceprt.php?file=fragment-32792.php

  25. greatbananas
    Member
    Posted 8 years ago #

    thanks, Kafkaesqui! your code works:

    <?php
    if(is_single()) :
    global $post; $id = $post->ID;
    foreach((get_the_category()) as $cat) :
    ?>
    <p><?php $my_query = $wpdb->get_results("SELECT $wpdb->posts.* FROM $wpdb->posts, $wpdb->post2cat WHERE category_id = '$cat->cat_ID' AND ID = post_id AND post_id <> '$id' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 10"); ?>
    <?php while(list(,$post) = each($my_query)) : ?>
    <h2 id="post-<?php the_ID(); ?>">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <?php the_title(); ?></a></h2>
    <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
    <?php endwhile; ?>
    </p>
    <?php endforeach; endif; ?>

    I was doing it a little different when using your previous code which was why it didn't work for me.

  26. greatbananas
    Member
    Posted 8 years ago #

    oops. I just realized this. When I use the code, it closes the comments on the post.

  27. Kafkaesqui

    Posted 8 years ago #

    "When I use the code, it closes the comments on the post."

    ???

    Since the code has nothing to do with comments, can you provide more information?

  28. greatbananas
    Member
    Posted 8 years ago #

    ok, I did more testing. When I place your code above:

    <?php comments_template(); ?>

    the comments doesn't show up and it says:

    "Comments are closed."

    It doesn't allow anyone to post comments and old comments does not show up. If I place your code under <?php comments_template(); ?> everything works fine.

  29. Kafkaesqui

    Posted 8 years ago #

    You're using it in The Loop? I wish I knew that before...what's happening is the code is clobbering the $post object, and comments rather needs it.

    Here's a version that should get along with its neighbors in or out of The Loop:

    http://guff.szub.net/wp-content/sourceprt.php?file=fragment-loop-friendly-32792.php

    I've not included a replacement for the_author() (which was commented out in your original). It can be collected, but would require another SQL query and such, and I didn't want to complicate the code unless you really really need it.

    Note: This is going to be a tad more query-happy than the previous, which benefits from the $post object's cache. If you can find a way of running it all outside The Loop and still fit in with your layout, I'd recommend that.

  30. Monika
    Member
    Posted 8 years ago #

    Kafkaesqui
    you made my day :)
    since two or more hours I'm studying the codex and get a headache and need more than one cup of coffee, because I need exactly this query for my categories

    ok it is monday and I hate monday

    if(is_category
    and the rest of your code

    and it works like a charm :)

    thanks a lot
    regards
    Monika

Topic Closed

This topic has been closed to new replies.

About this Topic