Ordering by meta_value AND date NOT WORKING with wp_query

You have to use the posts_orderby filter to do this at the mo, eg function wpse159469_posts_orderby( $orderby, $query ) { return implode( ‘ DESC,’, explode( ‘,’, str_replace( array(‘ ASC’, ‘ DESC’ ), ”, $orderby ) ) ) . ‘ DESC’; } then around your query: add_filter( ‘posts_orderby’, ‘wpse159469_posts_orderby’, 10, 2 ); $query = new WP_Query( … Read more

Fetch posts from current week (Sunday to Saturday)

You can try: ‘date_query’ => array( array( ‘year’ => date( ‘Y’ ), ‘week’ => strftime( ‘%U’ ), ), ), where %U is: Week number of the given year, starting with the first Sunday as the first week or ‘date_query’ => array( array( ‘after’ => strtotime( ‘last Sunday’ ), ), ),

SELECT * FROM $wpdb->posts WHERE ID > 160

SELECT * FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id) LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id) WHERE $wpdb->posts.post_type=”post” AND $wpdb->term_taxonomy.taxonomy = ‘post_tag’ AND $wpdb->posts.ID > 160 ORDER BY $wpdb->posts.ID ASC LIMIT 5 $pageposts = $wpdb->get_results($querystr); if ($pageposts): ?> <?php global $post; ?> <?php … Read more

wp_query with multiple custom fields

Stupid typo I’m afraid. This: array ( ‘key’ => ‘ys_product_start’, ‘value’ => date(‘Ymd’), ‘compare’ => ‘>=’ ), array ( ‘key’ => ‘ys_product_end’, ‘value’ => date(‘Ymd’), ‘compare’ => ‘<=’ ) Should be like this: array ( ‘key’ => ‘ys_product_start’, ‘value’ => date(‘Ymd’), ‘compare’ => ‘<=’ ), array ( ‘key’ => ‘ys_product_end’, ‘value’ => date(‘Ymd’), ‘compare’ => … Read more

Using pre_get_posts on a specific core/query block

A modified version of the suggestion from @birgire works. Note that this applies to the core/post-template block, not the core/query block. add_filter(‘block_type_metadata_settings’, function ($settings, $metadata) { if ($metadata[‘name’] !== ‘core/post-template’) { return $settings; } if ($settings[‘render_callback’] !== ‘render_block_core_post_template’) { return $settings; } $settings[‘render_callback’] = ‘wpse_render_block_core_post_template’; return $settings; }, 10, 2); function wpse_render_block_core_post_template($attributes, $content, $block) { … Read more

WP_Query for showing specific post by id

For multiple post IDs, in WP_Query arguments, we can use: ‘post__in’ => [ 272, 282, 292 ], but for a single post ID it’s: ‘p’ => 123, Note that the default post type is post. Checking out the SQL query It’s informative to checkout the generated SQL query of WP_Query. Try e.g. in the wp-cli … Read more