Only display a certain number of posts, dependent on how many posts there are available in a query

You don’t have to run two queries, as you already got what you want (even more). $args = array ( ‘post_type’ => ‘mytheme_posttype’, ‘post_status’ => ‘publish’, ‘numberposts’ => 12, ); $sliderQuery = new WP_Query($args); $num_posts = $sliderQuery->post_count; if ($num_posts >=4) { if ($num_posts >= 12) $postsToPrint = 12; elseif ($num_posts >=8) $postsToPrint = 8; else … Read more

How to orderby meta_value_num with dollar ($) sign

You could also use the posts_orderby filter: function wpse155827_posts_orderby_price( $orderby ) { return str_replace( ‘wp_postmeta.meta_value’, ‘substr(wp_postmeta.meta_value, 1)’, $orderby ); } add_filter( ‘posts_orderby’, ‘wpse155827_posts_orderby_price’ ); $theQuery = new WP_Query( array( ‘orderby’ => ‘meta_value_num’, ‘meta_key’ => ‘price’, ‘order’ => ‘ASC’, ‘suppress_filters’ => false, ) ); remove_filter( ‘posts_orderby’, ‘wpse155827_posts_orderby_price’ );

how to query multiple categories in wordpress?

Use like this query_posts( array( ‘category__in’ => array(5,1), ‘posts_per_page’ => 1, ‘orderby’ => ‘title’, ‘order’ => ‘ASC’ ) ); or $my_query = new WP_query(array(‘category__and’ => array(5,1))); while ($my_query->have_posts()) : $my_query->the_post();

How to get to a specific position in wp_query?

I think instead of using PHP rand you’re better off limiting ( if you need to ) using posts_per_page and then getting a random set using orderby => ‘rand’. Full List of Ordering Parameters So if I were to edit your query it would look like this: $args = array( ‘post_type’ => ‘attachment’, ‘meta_key’ => … Read more

How to show only posts with images?

No need for post IDs what has no thumbnail. Use meta query to get only those what has thumbnail. Add meta query function get_only_posts_with_images( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( ‘meta_query’, array( array( ‘key’ => ‘_thumbnail_id’ ) ) ); } } add_action( ‘pre_get_posts’, ‘get_only_posts_with_images’ ); Or use custom query. $query … Read more

Modifying date filter on admin page for custom post type to link to custom field

Use the pre_get_posts action to override the default query. In the admin, a date query will be the query var m in the format YYYYMM. Assuming your meta dates are stored the same as MySQL dates (YYYY-MM-DD), you can use the following: function wpse_189824_date_meta_query( $wp_query ) { if ( is_admin() && $wp_query->is_main_query() && $wp_query->get( ‘post_type’ … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)