Another whacky title but I like to have a bit of fun with you guys.
You maybe wondering what i mean in the title, but it is really quite simple. When you are building an array of arguments for a custom loop using get_posts or a new WP_Query you will be using post_status, post_type and a host of other possibilities. So you will be (I always am) tempted to use post_author to specify the author you want to show posts for. But this would be wrong, for post_author you must use “author” only like so.
$psts = get_posts(array('author' => get_current_user_id(),'post_type' => 'bookable_resource', 'post_status' => 'publish', 'numberposts' => -1, 'fields' => 'ids'));
Excuse the poor formatting but I wanted to keep this post short. The above also ties into something else I have been toying with a post about and that is trying the simple things first when code doesn’t work. For example if you used post_author in the above code to specify the author you wouldn’t get the intended results, at which point there is a tendency to assume the simple things are right and start looking for complex problems like is something filtering using pre_get_posts and breaking my code etc etc, but the problem is really simple – swapping post_author for author in the arguments array to get_posts. Anyway – hope it helps save anyone some time.
Like the code you see here? You can hire me at Codeable.io - a WordPress specialist outsourcing site that vets developers to make sure they are of the highest standard. See all developers you can hire through Codeable here.