WP_QUERY Get posts by category and similar name (Like)

Revisited and simplified answer: You can try: $args = [ ‘post_type’ => ‘post’, ‘post_status’ => ‘publish’, ‘category_name’ => ‘projects’, ‘_name__like’ => ‘proj*’ // <– our new input argument! ]; $my_query = new WP_Query( $args ); where we’ve created the _name__like input argument. It supports wildcard *, for example: ‘_name__like’ => ‘a*b*’ Note that draft posts … Read more

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

Get first post from wp_query

If you poke through WP_Query the set of queried posts is saved into posts property and current post gets assigned to post one (each time loop iterates). So you could do $connected->posts[0] if you need to just fetch that, but it might be more convenient to do $connected->the_post(); then $connected->post if you need to skip … 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

error code: 521