0

I want to query for posts with a certain meta value. I would use the WP_Query but when in a loop (correct me if i am wrong.. I am learning wordpress; is it only when in loop? is the the_post() that sets the globals?), it changes the global $post.

Is it possible to loop without setting up the global values? I just want to fetch some stuff in the posts, i don't want to use the_content, the_title, etc.. just raw rows of arrays?

If I want to query WP_Query with meta fields conditions and expect and want only post, how do I get it? Do I still need to loop?

1 Answer 1

3

The query itself doesn't change globals, however you are correct that the_post() does.

The most contained way to just retrieve posts (as array of results) is using get_posts() function, which is wrapper around WP_Query and takes same arguments.

8
  • yup i am actually using get_posts() which is just executing query() of Wp_Query. ok so if i don't use the_post() or setup_postdata, there's no need for me to use wp_reset_query or wp_reset_postdata right? as globals are untouched? btw: i am using your R Debug class which helped me a lot debugging and understanding some hooks in WP. it's great! will integrate it in my own custom framework for all my plugins development!
    – yeahman
    Commented Apr 21, 2014 at 18:43
  • sidenote: i am reading that I should use wp_reset_postdata() instead of wp_reset_query() (unless i am using query_posts() which is not recommended)
    – yeahman
    Commented Apr 21, 2014 at 18:54
  • 1
    @yeahman correct, unless you use anything that changes globals you do not need to reset afterwards. Also see wordpress.stackexchange.com/a/1755/847
    – Rarst
    Commented Apr 21, 2014 at 19:00
  • thx for your help and explanations! I am starting to get more and more familiar to WP :)
    – yeahman
    Commented Apr 21, 2014 at 19:06
  • and you recommend using a new instance of Wp_Query vs using the global $wp_query?
    – yeahman
    Commented Apr 21, 2014 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.