Better wordpress attachment query than this

I don’t have the data to test so the code below may be wrong. add_filter(‘posts_search’, ‘set_is_tax_to_true’ ,10,2); function set_is_tax_to_true($search,$query){ $query->is_tax = true; } $args = array( ‘post_type’ => ‘attachment’, ‘posts_per_page’ => -1, ‘post_status’ => ‘pending’, ‘post_parent’ => $post_id, ‘order’ => ‘ASC’, ‘orderby’ => ‘ID’, ‘meta_query’=> array( array( ‘key’ => ‘cp_sys_expire_date’, ‘value’ => current_time(‘mysql’), ‘compare’ => … Read more

How Can I Change The Tax Query For The Main Loop For Taxonomy Archives?

@birgire answer is fine (+1 from me), however $query->tax_query->queries can contain more than one taxonomy, and $query->tax_query->queries[0] can be the query for another taxonomy. So, if you want to set ‘include_children’ to false (note that it is a boolean argument) on the taxonomy ‘TAXONOMY NAME’, then you should be sure that you act on right … Read more

Limiting number of related posts

Build an array of categories: foreach ($categories as $category) { $cat[] = $category->cat_ID; } Then pass that to the query: $args=array( ‘category__in’ => $cat, ‘order’ =>DESC, ‘post__not_in’ => array($post->ID), ‘posts_per_page’=>2, ); In other words, put the query after the foreach, not inside of it.

Showing specific post in order of array wp_query

For anyone struggling with this. ‘orderby’=>’post__in’ makes this work. <?php $successful_musics_array = array(‘1682’, ‘1684’, ‘556’, ‘1793’, ‘558’, ‘1788’, ‘320’, ‘1190’, ‘1865’, ‘1125’, ‘1863’, ‘1540’); $query = new WP_Query( array( ‘post_type’ => ‘songs’, ‘posts_per_page’ => ’15’, ‘post__in’ => $successful_musics_array, ‘orderby’=>’post__in’ ) ); if ( $query->have_posts() ) : ?> <?php while ( $query->have_posts() ) : $query->the_post(); ?> … Read more

Custom query: Get all posts that are from a certain category?

it’s quite easy: $months = $wpdb->get_col(“SELECT DATE_FORMAT(post_date_gmt, ‘%m’) FROM $wpdb->posts LEFT JOIN $wpdb->term_relationships as t ON ID = t.object_id WHERE post_type=”post” AND post_status=”publish” AND t.term_taxonomy_id = 11 AND DATE_FORMAT(post_date_gmt, ‘%Y’) = $y GROUP BY DATE_FORMAT(post_date_gmt, ‘%Y-%m’) ORDER BY post_date_gmt DESC”); term_taxonomy_id is the category. And I join on the object ID

Check return value of get_posts

From previous questions, I know you would want optimize your queries. There is no need to change WP_Query into get_posts for perfomance. As I stated in a another post, yes, get_posts() is faster than WP_Query because get_posts() legally breaks pagination, but what get_posts() do can be done with WP_Query. To really understand what get_posts() does, … Read more

find posts that don’t have a custom taxonomy

Emily’s answer is mostly correct, just that the operator shouldn’t have an underscore in it: $args = array( ‘post_type’ => ‘my_post_type’, ‘tax_query’ => array( array( ‘taxonomy’ => ‘series’, ‘operator’ => ‘NOT EXISTS’, ), ), ); $query = new WP_Query( $args );

WP_Query infinite Loop

You need $form_enquiries->the_post() to “push” the loop onto the next post – otherwise yes, you get an infinite loop! while ( $form_enquiries->have_posts() ) { $form_enquiries->the_post(); // rest of your loop }