WordPress.org

Ready to get started?Download WordPress

Forums

query_posts in a reusable function? (6 posts)

  1. Doodlebee
    Member
    Posted 3 years ago #

    Hi all :)

    I had a reusable function that was working terrifically - it was a simple function where you'd input a category name and a number, and the function would take the category name, convert it to an ID, and pop that into a query_posts section (the number was to show how many posts). For example:

    $catid = get_cat_id($catname);
     query_posts(array('cat' => '$catid', 'showposts' => '$num', 'post_status' => 'publish'));

    However, I see that in WP 3.0.x, "showposts" has been deprectaed, and been replaced by "posts_per_page".

    My issue is that the function no longer works. It now simply pulls the most recent post of the chosen category, no matter what. I can get this code to work if I manually put it in the template file of my choosing, but that means that *anywhere* I want to use it, I have to rewrite this code, rather than use the nice, neat, reusable function.

    Has something happened so you can no longer use query_posts (and other versions thereof - I've tried using new WP_Query, parse_query($args), etc. all to no avail.)

    If it helps, here's the full code for the function. For the record, newsletter_date() is another function written, all it does is echo out a month and year - nothing more. I'm using it for comparison.

    So to call the function into the template file, you'd do something like this:

    <?php echo newsletter_cats('Bunko', '5'); ?>

    The idea would then be that the most recent 5 posts in the "Bunko" category would show up on the page.

    the function:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    Can anyone see why this can no longer work to show how many posts I need to output? All it's doing is returning the most recent post in the category. I've been trying for days to figure this out, and I'm at my wit's end.

    Thanks in advance for any help you can give.

  2. vtxyzzy
    Member
    Posted 3 years ago #

    The only thing I can see that even might be the cause is that you are passing a string for $howmany. I have never used a string as the value for posts_per_page, so I don't know if that is the problem or not.

  3. Doodlebee
    Member
    Posted 3 years ago #

    Well, when testing, I used a number (instead of a passed value) and it still didn't function.

    I found a workaround, but it required me basically to rewrite the function in another area. Works fine when I do that. So I have no idea what's up. When I printed out the end result, it was exactly what it should be. I had it narrowed down to the fact that *before* the "while", it was doing what it was supposed to be doing, but after the "while" part, it was not. i.e. if I printed out what was being pulled immediately after the "if(have_posts()): " part, it was exactly what I wanted, but if I printed after "the_post():" it was just pulling the one item. so somewhere between "if(have_posts())" and "the_post()" something is doing the funky dance.

    Very weird. I've chalked it up to a ghost in the machine. I hate that I had to rewrite it in a secondary spot (that's the point of writing a function - to reuse it), but it's working fine in the second spot. Makes no sense to me, but I'm letting it go!

  4. vtxyzzy
    Member
    Posted 3 years ago #

    Now I understand what is going on. You are using query_posts() inside The Loop. That is a no-no. You should use $myposts = new WP_Query(); instead. Of course, you will need the arguments to the query as before.

    Also, you might cause trouble using global $post;, but I am not sure about that since you are calling wp_reset_query();.

  5. Doodlebee
    Member
    Posted 3 years ago #

    No, I'm not using query_posts inside the loop. The loop begins at "if(have_posts())". My query is outside of that.

  6. vtxyzzy
    Member
    Posted 3 years ago #

    I had it narrowed down to the fact that *before* the "while", it was doing what it was supposed to be doing, but after the "while" part, it was not. i.e. if I printed out what was being pulled immediately after the "if(have_posts()): " part, it was exactly what I wanted, but if I printed after "the_post():"

    Sorry, I just misunderstood what you were saying in that text above.

Topic Closed

This topic has been closed to new replies.

About this Topic