Exclude posts with empty post_content in wp_query

This isn’t possible with a standard WP query, and you’ll have to leverage the use of posts_where before the WP_Query is called. function the_dramatist_filter_where($where=””){ $where .= ” AND trim(coalesce(post_content, ”)) <>””; return $where; } In the above, we’re simply only selecting posts where the column post_content isn’t empty. Then add the filter. add_filter(‘posts_where’, ‘the_dramatist_filter_where’); Now … Read more

WP Query—Relationship between two custom post types and their fields

I haven’t tested this but it SHOULD work. You’ll want to put it in the single-staff.php. You’ll have to add the details (actual query, post types, etc). $reports = get_posts(array( ‘post_type’ => ‘reports’, //use actual post type ‘meta_query’ => array( ‘relation’ => ‘or’, array( ‘key’ => ‘primary_contact’, // name of custom field ‘value’ => ‘”‘ … Read more

WP_Query attachments by multiple IDs

I think you need to use post__in . The code will be something like: $args = array( ‘post__in’ => array( 1, 2 ), ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’, ‘no_found_rows’ => true, ‘posts_per_page’ => 2, ‘post_status’ => ‘any’ ); $query = new WP_Query($args);

How can I order Wp_Query hierarchically?

You’ll need to use something other than a single WP_Query. You could use wp_list_pages() with a custom walker to get your desired structure. wp_list_pages in widget, sidebar, or wherever you want the list to appear: <ul class=”cpt-hierarchy”><!– or whatever class you like –> <?php wp_list_pages(array( ‘post_type’ => ‘cpt’, // replace with your cpt’s slug ‘title_li’ … Read more

WP Query related posts by current page Tag ID

get_the_tags() returns an array of tags name, ID, and more. You should store only IDs in an array, and use it in your query. $post_tag = get_the_tags ( $post->ID ); // Define an empty array $ids = array(); // Check if the post has any tags if ( $post_tag ) { foreach ( $post_tag as … Read more

Pass arguments to WP_Query using “set” method

In the last example (the one that’s not working for you), you’re instantiating an instance of the WP_Query class without passing in an array of query vars. That’s fine, but internally this means the query never actually takes place. You’re calling $q->set( … ), but that’s only setting a query variable, it doesn’t run the … Read more