Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are making this far too hard on yourself as I know all too well. When I started with WordPress I came to it as an advanced SQL developer so I wanted to code everything in SQL. Turns out though that you almost never need to use SQL with WordPress because it has it's own API with built-in URL-like query language complete with a caching system (the caching system means you don't need to optimize nearly as much as you might otherwise thinK.)</p> <p>Here's a standalone file you can drop in the root of your website (call it <code>test.php</code> maybe?) so that you can test this out, fiddle with options and see it work:</p> <pre><code>&lt;?php include "wp-load.php"; $year = 2010; $query = new WP_Query("post_type=any&amp;posts_per_page=-1&amp;year={$year}"); echo "&lt;h1&gt;Year: {$year}&lt;/h1&gt;"; echo '&lt;ul&gt;'; foreach($query-&gt;posts as $post) { echo "&lt;li&gt;{$post-&gt;post_title} [post_type={$post-&gt;post_type}]&lt;/li&gt;"; } echo '&lt;/ul&gt;'; </code></pre> <p>The query language I mentioned, extracted from above, looks like this:</p> <blockquote> <p>post_type=any&amp;posts_per_page=-1&amp;year=2010</p> </blockquote> <p>You could also query in an array form, for example (it really just depends on your use-case and/or your preference):</p> <pre><code>$query = new WP_Query(array( 'post_type' =&gt; 'any', 'posts_per_page' =&gt; -1, 'year' =&gt; $year, )); </code></pre> <p>There are a lot more query parameters for <code>WP_Query()</code> than the three I used above. You can find the best documentation for those parameters here:</p> <ul> <li><a href="http://codex.wordpress.org/Function_Reference/query_posts#Parameters" rel="nofollow noreferrer">Query Parameters on the WordPress Codex</a></li> </ul> <p>Note that <code>WP_Query()</code> is just one way to query in WordPress. There's also <code>query_posts()</code>, <code>get_posts()</code> and you can interact with the query during <em>hooks</em> as well. </p> <p>Yes, there are times when you need to drop to SQL but it's better in WordPress, especially for backward compatibility if you avoid SQL and use the APIs whenever possible.</p> <p>Hope this helped.</p> <p>-Mike</p> <p>P.S. Want more info on the WordPress query? Let me suggest you ask questions over at <a href="https://wordpress.stackexchange.com/">WordPress Answers</a> when it opens to full public beta in a week (WordPress Answers is from <a href="http://stackexchange.com/">StackExchange</a>, the same folks as StackOverflow.) WordPress Answers is more likely to get you better answers for WordPress than here because that site will be filled with WordPress experts as opposed the generalists (and otherwise extremely knowledgeable folks) here.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload