Include posts from some categories while excluding from others

You could use a Taxonomy Query, like so: $args = array( ‘tax_query’ => array( ‘relation’ => ‘AND’, array( ‘taxonomy’ => ‘category’, ‘terms’ => array(), ‘operator’ => ‘IN’ ), array( ‘taxonomy’ => ‘category’, ‘terms’ => array(), ‘operator’ => ‘NOT IN’, ), ), ); $query = new WP_Query( $args ); This will query posts that are in … Read more

Ordering posts by an array

This is not possible with native API and you will probably have a rather bad time hacking up SQL for it. This would be quite trivial to re–sort posts like this after they are retrieved with PHP code. if you use get_posts() you get array you manipulate already and for a WP_Query object array of … Read more

Can’t sort custom post type by random

You could use meta_query to search in the meta data, and then sort them as random: $args = array( ‘posts_per_page’ => 4, ‘post_type’ => ‘projekte’, ‘orderby’=> ‘rand’, ‘post__not_in’ => array( get_the_ID() ), ‘meta_query’ => array( array( ‘key’ => ‘ecpt_skills’, ‘value’ => $projektart, ‘compare’ => ‘LIKE’, ) ), ); There is also a mistype in your … Read more

Can I modify a WP_Query instance or clone it with modifications?

WP_Query can repeatedly loop through the retrieved posts multiple times, but it was never meant to query them repeatedly/iteratively. You can just do $query->query($args) again, but there is little point as opposed to just making a new clean object. In your case I think it would make sens to have your custom function accept optional … Read more

WP Query ‘posts_per_page’

You add the necessary parameters to ask only for posts with the relevant clinic ID. e.g. $q = new WP_Query( [ ‘post_type’ => ‘doctors’, ‘meta_key’ => ‘clinic_id’, ‘meta_value’ => get_queried_object_id(), ] ); However, you have a fundamental mistake in how the data structure is designed. Filterable fields are supposed to be stored as terms/taxonomies, not … Read more

Ignore latest two posts

You can use offset to exclude the latest posts. Your arguments can be like: $args = array( ‘posts_per_page’ => 5, ‘offset’ => 2, ‘post__not_in’ => array(827, 809), ‘post_status’=>”publish”, ‘post_type’=>”post”, ‘orderby’=>”post_date”, ‘cat’=>’-1, -8, -9, -7, -6, -5, -4′, ‘paged’=> $paged ); However as you mentioned, it will break the pagination. There is a workaround, as stated … Read more

Loop increase number

You can add a php variable and increment it every time the while loop runs $args = array( ‘post_type’ => ‘events’, ‘order’ => ‘ASC’, ); $wp_query = new WP_Query($args); ?> <script type=”application/ld+json”> { “@context”:”http://schema.org”, “@type”:”ItemList”, “itemListElement”:[ <?php $i=0; while ( have_posts() ) : the_post(); $i++; ?> { “@type”:”ListItem”, “position”:”<?php echo $i; ?>”, “url”:”<?php the_permalink(); ?>” … Read more

Meta Query doesn’t works as espected

Mariolex, Have you tried using meta_query? I wrote something quick and untested but it may do the trick: $args = array( ‘post_type’ => ‘posts’, ‘showposts’ => ‘-1’, ‘meta_query’ => array( array( ‘key’ => ‘pickup_address’, //meta_key ‘value’ => ’43 Longview Drive, Papamoa’, //meta_value ‘compare’ => ‘LIKE’, //compare ), ), ); $query = new WP_Query( $args ); … Read more

Display an image if odd number of posts in grid

The common mistake people make in this situation is they start to introduce bunch of custom counters to keep track of state. Really WP_Query just has those already. In your case that would be global $wp_query and $wp_query->post_count and $wp_query->current_post. That should be quite enough to figure out even/odd overall and current iteration in the … Read more