WP 5.8 “Query Loop” block: where to place custom query?

Looking into render_block_core_post_template() we can see it calls build_query_vars_from_query_block() (previously named construct_wp_query_args) to setup the query arguments of WP_Query from the Query` block properties. From there I don’t see it supporting custom taxonomies for the secondary query … yet! Work-around-idea: For the Query Loop: add a search keyword for using custom taxonomies, e.g. :query-motor-electric: and … Read more

Is it better practice to use query_posts, WP_Query, or get_posts to create various custom loops within a Page?

Hi @janoChen: If you have a choice, go with WP_Query. Both of the other functions (query_posts() and get_posts()) call WP_Query indirectly. The former is designed to allow you to modify the main query after the standard query has already been run, for example when you want a second loop. But query_posts() affects global variables and … Read more

Sort posts alphabetically by custom field value, insert divider between different letters

Try this: <ul class=”list-ensemble”> <?php query_posts(‘post_type=person&post_status=publish&meta_key=last_name&orderby=meta_value&order=ASC’); $current_letter=””; if ( have_posts() ) while ( have_posts() ) : the_post(); $last_name = get_post_meta( $post->ID, ‘last_name’, true ); $letter = strtolower( substr( $last_name, 0, 1 ) ); if ( $letter != $current_letter ) { $current_letter = $letter; ?> <li class=”letter”> <img src=”https://wordpress.stackexchange.com/questions/10561/<?php echo $letter; ?>.jpg” alt=”<?php echo $letter; ?>” … Read more

How to order posts by descending comment count on taxonomy page?

A friend helped me with a solution that I could place in my functions.php file: add_filter( ‘pre_get_posts’, ‘my_pre_get_posts’ ); function my_pre_get_posts( $query ) { if ( is_tax( ‘locations’ ) && empty( $query->query_vars[‘suppress_filters’] ) ) $query->set( ‘orderby’, ‘comment_count’ ); return $query; }

Problem with ‘post__not_in’

You are trying to supply an array as part of the string query parameter. You could instead just supply the arguments list as an array like this: $newQuery = new WP_Query( array( ‘posts_per_page’ => 5, ‘orderby’ => ‘rand’, ‘cat’ => $cats[0]->term_id, ‘post__not_in’ => array($exclude_post) ) );

Query posts: how to exclude results if post is in multiple categories

This is probably not a “pure” way to do it, but this code make an array of featured post ids and then excludes them from future get_posts. $featuredPosts = get_posts(‘category’=>’D’,’numberposts’=>’-1′); $fPId = array(); foreach ($featuredPost as $currentPost) { array_push($fPId,$currentPost->ID); } $aPosts = get_posts(‘category’ => ‘a’, ‘exclude’=>$fpId); $bPosts = get_posts(‘category’ => ‘b’, ‘exclude’=>$fpId); $cPosts = get_posts(‘category’ … Read more

Limiting query_posts to 1, regardless of sticky post?

$GLOBALS[‘wp_query’]->found_posts will give you the number of posts. $GLOBALS[‘wp_query’]->posts is an array with all the posts found. So instead of while ( have_posts() ) : the_post(); use: setup_postdata( $GLOBALS[‘wp_query’]->posts[0] ); get_template_part( ‘content’, ‘super’ ); This way you don’t run through all post, you really just use one. And please read When should you use WP_Query … Read more

query_posts() in function makes global $wp_query out of sync?

Update with a better example header( ‘Content-Type: text/plain;charset=utf-8’ ); error_reporting( E_ALL | E_STRICT ); function failed_unset() { // Copy the variable to the local namespace. global $foo; // Change the value. $foo = 2; // Remove the variable. unset ( $foo ); } function successful_unset() { // Remove the global variable unset ( $GLOBALS[‘foo’] ); … Read more