How to list some posts first in the loop based on post id

If you need to: page the query retain 12 posts per page instead of “sticking” the desired posts on top of the required 12 only need to show those posts on the first page you can try the following $ids_args = [ ‘post_type’ => ‘products’ ‘posts_per_page’ => -1, ‘orderby’ => ‘meta_value_num’, ‘meta_key’ => ‘_price’, ‘order’ … Read more

Pagination on a WP_query not showing navigation links

Please do not use showposts it got replaced by posts_per_page ages ago. Personally I would add the arguments to the WP_Query like shown below, additionally pagination should work like shown below: $paged = ( get_query_var( ‘paged’ ) ) ? get_query_var( ‘paged’ ) : 1; $args = array( ‘posts_per_page’ => 4, ‘paged’ => $paged, ); $the_query … Read more

Conditional arguments for WP_Query and tax_query depending on if $somevar has a value

You can define the args outside of the WP_Query instantiation: <?php $tax_query = array(‘relation’ => ‘AND’); if (isset($search_course_area)) { $tax_query[] = array( ‘taxonomy’ => ‘course-area’, ‘field’ => ‘id’, ‘terms’ => $search_course_area ); } if (isset($search_course_level)) { $tax_query[] = array( ‘taxonomy’ => ‘study-levels’, ‘field’ => ‘id’, ‘terms’ => $search_course_level ); } if (isset($search_course_mode)) { $tax_query[] = … Read more

WP_Query: Exclude hidden products from WooCommerce product list

Important: The following only works for WooCommerce versions less than 3.0. For a more up-to-date answer please see the other answer by kalle. WooCommerce save this data as metadata so you’ll need to run a Meta Query against the name _visibility. Something like: ‘meta_query’ => array( array( ‘key’ => ‘_visibility’, ‘value’ => ‘hidden’, ‘compare’ => … Read more

Get posts from Network (Multisite)

I created a plugin which does something similar (called Multisite Post Display https://wordpress.org/plugins/multisite-post-reader/ ) . It displays posts from all multisite sub-sites. The code in there might be helpful for what you are doing. You are welcome to dig into it and use the code to help with your project. (After all, I used other … Read more

WP_Query orderby one custom field then another in one query

According to https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters you want to specify it similar to the following code example: $args = array( ‘orderby’ => array( ‘title’ => ‘DESC’, ‘menu_order’ => ‘ASC’ ) ); $query = new WP_Query( $args ); This is supported from WordPress 4.0. For custom fields (post meta) you will need an added meta query, as detailed on … Read more