Title: query_posts in a reusable function?
Last modified: August 19, 2016

---

# query_posts in a reusable function?

 *  [Doodlebee](https://wordpress.org/support/users/doodlebee/)
 * (@doodlebee)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/query_posts-in-a-reusable-function/)
 * 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](http://codex.wordpress.org/Forum_Welcome).
   Please use the [pastebin](http://wordpress.pastebin.com)]_
 * 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.

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

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/query_posts-in-a-reusable-function/#post-1861726)
 * 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.
 *  Thread Starter [Doodlebee](https://wordpress.org/support/users/doodlebee/)
 * (@doodlebee)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/query_posts-in-a-reusable-function/#post-1861728)
 * 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!
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/query_posts-in-a-reusable-function/#post-1861732)
 * 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();.
 *  Thread Starter [Doodlebee](https://wordpress.org/support/users/doodlebee/)
 * (@doodlebee)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/query_posts-in-a-reusable-function/#post-1861734)
 * No, I’m not using query_posts inside the loop. The loop begins at “if(have_posts())”.
   My query is outside of that.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/query_posts-in-a-reusable-function/#post-1861735)
 * > 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.

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

The topic ‘query_posts in a reusable function?’ is closed to new replies.

## Tags

 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [15 years, 4 months ago](https://wordpress.org/support/topic/query_posts-in-a-reusable-function/#post-1861735)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
