query if page has not child

$args = array( ‘post_status’ => ‘publish’, ‘post_parent’ => $post_id ); $children = get_posts( $args ); if( count( $children ) > 0 ) { //has children } else { //does not have children } Docs: get_posts() This should also allow you to have access to the data of the children, should you need it. If you … Read more

Order posts by recent comments

You can use the following code, which first gets the most recent comments, then using the post_ids associated with those comments, queries the database for those posts: // Get the 5 most recent comments $recent_comments = get_comments( array( ‘orderby’ => ‘comment_date’, ‘number’ => 5 ) ); // If there are any posts with comments if … Read more

Can’t seem to do combined query AND sort?

I had this same problem until I realized you don’t actually have to query by the event start date, only the end date. As long as the event’s end date has not past it doesn’t matter whether the start date is in the future or not. All you have to do is grab all the … Read more

Get post ids sorted by meta_key

It’s easy. 🙂 You would use get_posts function. See WP_Query class for complete list of parameters that you can use. function get_posts_by_view_count() { $ids = array(); $args = array( ‘orderby’ => ‘meta_value_num’, ‘order’ => ‘DESC’, ‘meta_key’ => ‘post_view_count’ ); $posts = get_posts( $args ); if( $posts ) { foreach( $posts as $post ) { $ids[] … Read more

Transient api Caches confused

“This code works” – but it doesn’t! If you’re gonna cache paginated posts, you’ll need to store them chunked: if ( ! $my_paged = absint( get_query_var( ‘paged’ ) ) ) $my_paged = 1; if ( ! $my_query = get_transient( “recent_$paged” ) ) { $my_query = new WP_Query( “cat=3&posts_per_page=5&paged=$my_paged” ); set_transient( “recent_$paged”, $my_query, 60 * 60 … Read more

List author’s posts with SQL

Your table names are wrong. note in your first query the use of $wpdb->posts to reference the posts table. however– this is a very simple query that can be done via the API rather than writing SQL queries from scratch. Use a new WP_Query instance instead.

wp_list_pages() refuses to output posts

wp_list_pages will only output hierarchical post types. If we look at the function in source, we’ll see that it calls get_pages to load pages for output. If we look at get_pages function in source, we’ll see: // Make sure the post type is hierarchical $hierarchical_post_types = get_post_types( array( ‘hierarchical’ => true ) ); if ( … Read more

Get all custom post types excepted some…

Following code generates the array to use as argument. <?php // Excluded CPTs. You can expand the list. $exclude_cpts = array( ‘portfolio’ ); // Builtin types needed. $builtin = array( ‘post’, ‘page’, ‘attachment’ ); // All CPTs. $cpts = get_post_types( array( ‘public’ => true, ‘_builtin’ => false ) ); // remove Excluded CPTs from All … Read more