list posts and wrap them alphabetically A-Z

I would do it the other way around- loop over the alphabet and check all posts for a match with each letter. $all_posts = new WP_Query( array( ‘orderby’ => ‘title’, ‘order’ => ‘ASC’, ‘posts_per_page’ => -1 ) ); if( $all_posts->have_posts() ){ foreach( range( ‘A’, ‘Z’ ) as $letter ) { echo ‘<div class=”group_letter”><div class=”letter”>’ . … Read more

WP Query to displaying date and posts for that date?

I was able to do this using the following: $args = array( ‘post_type’ => ‘show’, ‘post_status’ => ‘publish’, ‘posts_per_page’ => -1, ‘orderby’ => ‘meta_value’, ‘meta_key’ => ‘show_date’, ‘order’ => ASC ); $query = new WP_Query( $args ); if( $query->have_posts() ) { $day_check = ”; while( $query->have_posts() ): $query->the_post(); $day = date(‘Y-m-d’, strtotime(get_field(‘show_date’))); if ($day != … Read more

Get taxonomy terms only of the WP_Query current posts

You’d probably do something like this $args = array( ‘post_type’ => ‘product’, ‘product_cat’ => get_queried_object() ); $query = new WP_Query( $args ); $termsInPosts = array(); if ( $query->have_posts() ) { while( $query->have_posts() ) : $query->the_post(); $postTerms = wp_get_post_terms(get_the_ID(), ‘brands’, array(‘fields’ => ‘term_id’)); foreach ($postTerms as $postTerm) if (!in_array($postTerm->term_id, $termsInPosts)) $termsInPosts[] = $postTerm->term_id; endwhile; } $marcas_terms … Read more

WP_Query is printing out only one post when posts_per_page is set to multiple

you are re-initializing the out variable that is why it shows only one product // Custom Post Type for Use Cases page slider function create_post_type() { register_post_type( ‘use_cases’, array( ‘labels’ => array( ‘name’ => __( ‘Use Cases’ ), ‘singular_name’ => __( ‘Use_Case’ ) ), ‘public’ => true, ‘has_archive’ => true, ) ); } add_action( ‘init’, … Read more

Undefined property: WP_Post_Type::$ID

Try changing the get_queried_object()->ID to get_queried_object_id(). All the get queried object may not have a property name $ID. So this get_queried_object_id gives the $queried_object_id property value, which gets set on instantiation of WP_Query class. If the $queried_object_id isn’t set then it’ll give you 0.

Sorting Posts | Alphabetical Order

Have you read this thread? Display all posts starting with given letter? This wouldn’t be simple to do with WP_Query and that’s unfortunate because that would help preserve pagination. // Rules $args = [‘post_type’=>’post’,’orderby’=>’name’]; // The Query $query1 = new WP_Query( $args ); // The Loop while ( $query1->have_posts() ) { $query1->the_post(); echo ‘<li>’ . … Read more

storing wp_head in a variable?

This function doesn’t take any params, so there is no nice way of doing it. But of course you can do this PHP way using output buffering 😉 ob_start(); wp_head(); $var = ob_get_clean();