• Hi everyone thanks for all your help.

    I am attempting to modify my template to display different sets of posts based on the GET data sent to my page. This is an extract of the code I’m using

    if ($_GET['test']!="") {
    	query_posts('meta_key=author');
    } else {
    	query_posts('category_name=Online Courses');
    }

    When there is no GET value for “test”, the page works fine, the posts from category Online Courses are loaded and properly display in the loop below.

    When “test” is specified, the code returns an empty result set. When I replace
    query_posts('meta_key=author');
    with
    get_posts('meta_key=author');

    and print the output, I can see that get_posts is returning the proper result set.

    Any ideas? I am using the exact same parameters on both get_posts and query_posts, but one returns the proper set and one returns an empty set.

    Thanks in advance!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hey David,

    Sorry, I don’t have an answer… Because I’ve got a similar problem…
    See my post

    So, yeah, I’ve also noticed weird behaviour concerning ‘query_posts()’ vs. ‘get_posts()’…. What’s going on here….?

    Greetz!

    Thread Starter davidcroda

    (@davidcroda)

    Thanks for your replies guys.

    Unfortunately that link doesn’t help me Michael because I am properly using query_posts to override my main loop for the page. It works with the category parameter.

    I am merely getting the results from get_posts to show that posts do exist with the specified meta_key value, but they are not being returned by query_posts.

    The main problem is that two identically formed queries, one with get_posts, and one with query_posts, are returning different results.

    Thread Starter davidcroda

    (@davidcroda)

    Just wondering, but as a hack, could I use get_posts to return the result set I want, and then override the $posts variable with this result. Would this allow the loop to function as normal with the new result set? This is an example of what I’m talking about.

    would this
    $posts = get_posts(‘meta_key=author’);
    START LOOP

    Display the same as: (assuming they returned the same results)

    query_posts(‘meta_key=author’);
    START LOOP

    Thread Starter davidcroda

    (@davidcroda)

    Update:

    This hack does work.

    I still don’t know why query_posts doesn’t return the proper results though

    You would use setup_postdata with get_posts.

    Pretty good description of get_posts vs. new WP_Query
    https://stackoverflow.com/questions/570152/what-is-the-best-method-for-creating-your-own-wordpress-loops

    Thread Starter davidcroda

    (@davidcroda)

    MichaelH,

    I really appreciate your help, but the issue here is that query_posts and get_posts return different results.

    query_posts(‘meta_key=author’); is not returning the posts with the meta key “author” set. get_posts(‘meta_key=author’); works properly.

    As far as I can tell this is a bug. If there is a better place for me to post this so the developers can look at this case, let me know.

    Thanks

    Reporting Bugs explains entering a bug report in Trac.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘query_posts and get_posts return different results’ is closed to new replies.