difference between like ‘%%%var%%’ and ‘%var%’

Firtly, in SQL the % symbol is a “wildcard operator”. When used in a LIKE statement, it means “0 or more characters”. So LIKE ‘Hello%’ means any string beginning with Hello, including just Hello. While LIKE ‘%Hello%’ means any string that contains Hello, either at the beginning, middle, or end. $wpdb->prepare() on the other hand … Read more

Get number of authors

This reply was pretty low on the list, on the article you referred to; but it will do the job: $users_count = count(get_users(array(‘fields’=>array(‘ID’),’role’=>’author’))); Originally posted here, buy shanebp

How to use offset in WP_Query

offset is one of the arguments that you can pass to WP_Query, so it belongs in the $args array: $args = array( ‘post_type’ => ‘post’, ‘category_name’ => ‘category’, ‘orderby’ => ‘date’, ‘order’ => ‘DESC’, ‘showposts’ => 4, ‘offset’ => 4, ); PS: $wp_query is a reserved variable used by the main query. When creating your … Read more

Need wp_query to return all children and grandchildren

Ok, I found a solution. I first looped through and got all the child page ID’s, created an array, and used post_parent__in <?php $parentarray = array($parentid); $parentposts = new WP_Query( array( ‘posts_per_page’ => -1, ‘post_type’ => ‘page’, ‘post_parent’ => $parentid ) ); while ( $parentposts->have_posts() ): $parentposts->the_post(); ?> <?php $parentarray[] = get_the_id(); ?> <?php endwhile; … Read more

WordPress Related Post by tags in Single.php

The code shown isn’t working as wished, what is wrong? you’re using only the first tag : $first_tag = $tags[0]->term_id; to grab posts from all tags you have to iterate the whole array until a max of 5. The code below ( untested) grabs a max of 25 posts theoretically using up to 5 tags..BUT … Read more

if statement in wp_query arguments

As said here : Conditional arguments for WP_Query and tax_query depending on if $somevar has a value I 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[] = … Read more

How to vary post loop results layout and resume?

How do I get a WP_Query object containing the next five posts? : // $query is the original WP_Query object. $five_posts = new WP_Query(); $five_posts->posts = array_slice( $query->posts, $query->current_position, $query->current_position + 5 ); How do I then elegantly resume the standard-layout loop output at the right place? : /* Sum the count of all the … Read more