Query the database after get_header() has been included

Your question is vague: i’ll try to be as specific as possible. The basic rule is: if you need to modify the default query, use pre_get_post If you need to display another list of results than the one linked to your template url, use get_posts. I find this documentation to be quite useful http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/ Doc … Read more

WP MS: How to query over the network

[*] Each site in a Multisite network is considered to be exactly that — a separate site. All the $wpdb values (eg, $wpdb->posts) are distinct for each site. You can use switch_to_blog() and restore_current_blog() to get content (posts, etc) from the various sites[*] in your network: $sites = array( 1, 2, 3, ); $all_posts = … Read more

Undefined variable: row?

First, it’s not $wpdb->wp_posts, it’s just $wpdb->posts. Second, you’re using prepare() incorrectly. You use prepare when you have some variable data that you need to safely insert into the query string, not otherwise. Using prepare on a known string does nothing and throws a warning. For example: $data=”example string” $query = $wpdb->prepare(“SELECT post_title FROM {$wpdb->posts} … Read more

How to SQL query posts IDs by categories AND authors?

Not the best solution 😛 but this could help you: select distinct(p.ID) from wp_posts p, wp_term_relationships r, wp_term_taxonomy tt, wp_terms t WHERE p.post_author in (1, 2, 3) and r.object_id = p.ID and tt.term_taxonomy_id = r.term_taxonomy_id and tt.taxonomy = ‘category’ and tt.term_id = t.term_id and t.term_id in (1, 2, 3);

Query to view scheduled or draft post

First, don’t use query_posts(). Note: This function isn’t meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. http://codex.wordpress.org/Function_Reference/query_posts The … Read more

Order terms by count – missing terms

By default hide_empty is true for get_terms. So i guess you must be missing terms which do not have posts. Try this, <?php $terms = get_terms(“autorzy”, array(‘orderby’ => ‘count’, ‘order’ => ‘DESC’,’hide_empty’=>0 )); $count = count($terms); if ( $count > 0 ){ echo “<div>”; foreach ( $terms as $term ) { ?> <?php echo $term->name; … Read more

How Do I Delete WordPress Posts Older Than 400 Days, From A WordPress Category

Addition note and warning: always always always take backups before doing anything in your database. It’s very easy to do something you didn’t mean to and lose tons of data, especially when dealing with DELETE statements. Also, if your database is using InnoDB for the database engine, you can use transactions via BEGIN, ROLLBACK, and … Read more