How to query custom post types with multiple keys?

Here is example from Codex on using BETWEEN with new format of meta query, which covers multiple fields as well: $args = array( ‘post_type’ => ‘product’, ‘meta_query’ => array( array( ‘key’ => ‘color’, ‘value’ => ‘blue’, ‘compare’ => ‘NOT LIKE’ ), array( ‘key’ => ‘price’, ‘value’ => array( 20, 100 ), ‘type’ => ‘numeric’, ‘compare’ … Read more

List one post only from each subcategory using get_posts?

I’m not sure this is the best way but it works: //First get all child categories if ‘big_corporates’ $categories = $args = array( ‘parent’ => $big_corporates_ID, ‘orderby’ => ‘name’, ‘order’ => ‘ASC’, ‘hide_empty’ => 1, ‘taxonomy’ => ‘projects_category’ ); //then loop over them and get the first post of each: echo ‘<ul>’; foreach ($categories as … Read more

Load the last post of a category

Hook template_redirect and check if it’s a category archive page, if so get the category ID from query_vars and query_posts for a single post from that category, which by default should be the most recent, then wp_redirect to the post’s permalink. This code would go in your theme’s functions.php file: function my_category_redirect() { if ( … Read more

get all categories’ latest post in one query

Quantity of queries isn’t the only measure of performance, an individual query can cost more than numerous simple queries. The cost of (small query) x12 < cost of large query x1 + string/array manipulation You would be better doing the 12 individual queries. If it is costly, store the result in a transient for a … Read more

get_posts not working on homepage?

The get_posts() argument is supposed to be numberposts not posts_per_page and you were passing it as string. Try making it an integer. function listposts( $atts ) { $atts = shortcode_atts( array( ‘tag’ => ‘jan-2011’, ‘numberposts’ => -1, ‘orderby’ => ‘post_date’, ‘order’ => ‘asc’, ‘post_status’ => ‘any’ ), $atts ); $catposts = get_posts( $atts ); $out=””; … Read more

Get posts in archive page

Instead of using the number 3, you get the current category by doing the following: $cat_ID = get_query_var(‘cat’); $posts = get_posts(‘cat=”.$cat_ID.”&numberposts=5&offset=0’);

Querying a query

// store the IDs of the posts we’ve already displayed to prevent duplicates $used_ids = array(); $highlights_posts = request_highlights($slides_id); foreach( $highlights_posts as $post ) { setup_postdata($post); // check if we’ve already done this post if(in_array($post->ID,$used_ids)){ // skip to the next one continue; } // we haven’t done this post before, add it’s ID to the … Read more

Magazine style frontpage with multiple categories/loops and no duplicate posts

I wrote a class specifically for passing data between widgets classes using static variables and it will work for your use case as well. Grab the class here. The way you would use this is as follows for each custom loop: $exclude_ids = IntermediaryData::get(); //Retrieve previous exclude IDs $args[‘post__not_in’] = $exclude_ids; //new WP_Query($args); Custom loop … Read more

paginate posts on admin page

This is what ended up solving it (I’ll include the relevant parts) $total = wp_count_posts()->publish; $perpage = 10; $curpage = isset( $_GET[‘pagenum’] ) ? intval($_GET[‘pagenum’]) : 1; global $post; $args = array( ‘post_type’ => ‘post’, ‘post_status’ => ‘publish’, ‘order’ => ‘DESC’, ‘orderby’ => ‘date’, ‘posts_per_page’ => $perpage, ‘offset’ => $perpage*($curpage-1) ); $pages = ceil($total/$perpage); $dash_posts … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)