heres my file thats running the query maby its another thing causing problems - http://pastebin.com/4Cw3xMEM

this functions as a direct query in SQLyog

SELECT * FROM wp_posts
WHERE post_status = 'publish' 
AND post_type = 'post'
AND YEAR(post_date)=2010
ORDER BY post_date DESC;

it returns the 3 posts that are from 2010 however when i attempt run it via my wordpress it pumps out all of the posts, no matter date

i understand there is a couple of items to be achieved therefore it chooses the right tables and so far as i'm able to gather, the next 'should' be okay

SELECT wposts.* 
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.post_status = 'publish' 
AND wposts.post_type = 'post'
AND YEAR(wposts.post_date)=2010
ORDER BY wposts.post_date DESC;

i truly dont comprehend the ' wposts. ' whatsoever, however it uses this kind of factor within the good examples using blogging platforms codex

the best goal is to create a query to drag posts froma certain year, and employ that to create Wordpress display categories of posts each year

i cant find something that helps let you know that to correctly call the querys while using wordpress structure

if a person can explain a couple of of those things it could help alot, example, using the following:


i've no clue what '->' is perfect for, i've only seen it utilized in php for setting areas of an assortment inside a foreach

  • how do you understand this factor to operate?

You're making this way too difficult on yourself when i know very well. After I began with WordPress I found it as being a professional SQL developer and so i desired to code my way through SQL. Works out though that you simply rarely want to use SQL with WordPress since it has it's own API with built-in URL-like query language including a caching system (the caching system means you don't have to optimize nearly around you may otherwise thinK.)

Here is a stand alone file you are able to stop by the main of the website (refer to it as test.php maybe?) to ensure that you can look at this out, fiddle with options and find out it work:


include "wp-load.php";

$year = 2010;

$query = new WP_Query("post_type=any&posts_per_page=-1&year={$year}");
echo "<h1>Year: {$year}</h1>";
echo '<ul>';
foreach($query->posts as $post) {
    echo "<li>{$post->post_title} [post_type={$post->post_type}]</li>";
echo '</ul>';

The query language I pointed out, removed previously mentioned, appears like this:


You might query in a wide array form, for instance (it truly just is dependent in your use-situation and/or perhaps your preference):

$query = new WP_Query(array(
    'post_type' => 'any',
    'posts_per_page' => -1,
    'year' => $year,

You will find much more query parameters for WP_Query() compared to three I made use of above. You'll find the very best documentation for individuals parameters here:

Observe that WP_Query() is simply one method to query in WordPress. Additionally, there are query_posts(), get_posts() and you will communicate with the query throughout hooks too.

Yes, you will find occasions when you really need to decrease to SQL but it is better in WordPress, specifically for backward compatibility should you avoid SQL and employ the APIs whenever you can.

Hope this assisted.


P.S. Want more information around the WordPress query? Allow me to suggest you request questions at WordPress Answers if this opens to full public beta per week (WordPress Solutions comes from StackExchange, exactly the same folks as StackOverflow.) WordPress Solutions is more prone to enable you to get better solutions for WordPress than here because that website will contain WordPress experts compared the generalists (and otherwise very knowledgeable folks) here.