Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

If we pass a String to functions like get_posts, and that string is not a meaningful querystring (for Example get_posts(' ')), the function returns the results of the default query (the last five posts or such). Is it possible either to suppress this behaviour or - even better - to find out, if the string was a meaningful querystring or if the function just fell back to default hence it was not?

Changing the default query-settings is not a solution because I don't want to affect the main loop, but only a function call of get_posts in a plugin.

share|improve this question
2  
Why don’t you pass a sanitized string to the function? –  toscho Jun 16 '13 at 13:02
    
To be honest, I think the main reason I started to think about that, was, that I tried different Querystrings and misspelled them quite often (or got failed with the right parameters) and got always confused, because it returned some posts anyway... –  John Trash Jun 16 '13 at 20:57

1 Answer 1

up vote 2 down vote accepted

If you look at how the query works, this is not a case of ...

  1. Check to see if the passed query conditions return posts
  2. Else run a fallback query

What happens is that your conditions are merged with certain defaults via a very complicated sequence of conditionals. At the end of that, you have the SQL that runs to get your posts. The system doesn't check to see if your conditions are "crap" and then decide to use them or not. Those conditions just get merged into the query in a way that generates a valid SQL statement. I am not sure how the query would know what is and isn't "crap" anyway.

The only thing I can think of doing would be to hook to pre_get_posts and check the query vars according to some set of conditions that you define to determine "crap" or "not crap".

As for the particular example you provide, get_posts is intentionally written to retrieve the latest post if not passed any arguments.

new WP_Query('') will not return anything, by the way, but essentially I agree with @toscho's suggestion in a comment-- pass a sanitized string. Know what you are sending to the function before you send it. If the string is empty or doesn't meet some other requirement, don't call the get_posts function at all.

share|improve this answer
    
Okay, thank you for the explanation - obviously it works a little bit different than I thought, so my question is obsolete. –  John Trash Jun 16 '13 at 20:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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