bruce

WordPress Post Status for WP Query: Unexpected Results?

I’ve run into this a few times.

You’re on a website, you notice that results seem to be different when you query the database depending on if you are on Page A, vs Page B — or if you are logged in vs logged out.
You start asking questions like “is my plugin broken?” and “did I introduce a bug or something”?

Answer is probably “no”.

Many things depend on WP_Query within WordPress. It has some out of the box functionality that you may not know about.

Straight from the Class Reference/WP Query « WordPress Codex:

post_status (string / array) – use post status. Retrieves posts by Post Status. Default value is ‘publish’, but if the user is logged in, ‘private’ is added. Public custom statuses are also included by default. And if the query is run in an admin context (administration area or AJAX call), protected statuses are added too. By default protected statuses are ‘future’, ‘draft’ and ‘pending’.

Typing

What does that mean? It means the following things — by default:

  • If you are not logged in, you will see only Published posts and any custom statuses that you may have made are also included.
  • If you are logged in, you will see Published AND Private posts.
  • If you have a query that executes from the Administration area OR is an AJAX call, then anything marked as Future, Draft or Pending will now show up.

Why is this important to know?

You have an event calendar right? Probably. People love that shit.

Maybe you have events that are set to Pending, or scheduled to be published in the Future but you didn’t want them public just yet… well if you have a developer who has made a fancy event listing page or fancy event calendar and it’s using AJAX — if they failed to specify that they ONLY want posts that are published and nothing else, then some of the above will show up.

Another situation could be that you are trying to figure out why something may or may not be showing up. You will have different Quality Assurance experiences as a logged in users vs a logged out user.

Leave a Reply