How to Query in WordPress which shows Alphabetic Posts?

Assuming the letters are links, then put the letter into the URL as a parameter. E.g.: http://example.com/brands/?letter=N Then, in the code (inside whichever template file will be used here), you need to query the DB for posts/brands beginning with ‘N’. You can either create a taxonomy for letters (https://codex.wordpress.org/Function_Reference/register_taxonomy) and assign each post to a … Read more

Does WP_Query ‘responds’ badly to empty arguments?

I would not write it the same way, because the more cluttered a query gets, the more likely it is to return unexpected results. Here’s how I’d handle your code: $args = array(); // All your WP_Query args go here. // You don’t need to pass ‘post_status’ => ‘publish’ because that’s default // Replace all … Read more

WP_Query to get post on frontpage

If you’re trying to get a post with the id of 32, the parameter you would use is p, which is one of the Post & Page Parameters. tag_id is used for querying posts with a particular Tag ID. $args = array( ‘post_type’ => ‘post’, ‘p’ => 32 ); $fp_post = new WP_Query( $args ); … Read more

How to create a widget like this?

Your question is a little short on detail, but let’s suppose you have retrieved the posts using wp_query and are looping through it like this: $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); STUFF } wp_reset_postdata(); } In the position of STUFF you want to distinguish … Read more

How to display a list of posts in same child category as current post

I just figured this one out by modifying the first foreach statement. $post_id = get_the_ID(); $cat_ids = array(); $categories = get_the_category(); if ( $categories && !is_wp_error( $categories ) ) { foreach( $categories as $category ) { if( 0 == $category->parent ) { continue; }else{ array_push( $cat_ids, $category->term_id ); } $current_post_type = get_post_type( $post_id ); $args … Read more

How can I split my query result in 2 arrays?

There’s no need to split the array, you can just close the first div once you’ve counted up to four elements of the array. $args = array( ‘post_type’ => ‘event’, ‘meta_key’ => ‘date’, ‘orderby’ => ‘meta_value_num’, ‘order’ => ‘ASC’, ‘posts_per_page’ => 8, ‘meta_query’ => array( ‘key’ => ‘date’, ‘value’ => date(‘Y-m-d’,strtotime(“today”)), ‘compare’ => ‘>=’, ‘type’ … Read more

Using a Shortcode to List Posts from Multiple Categories

Maybe you can make it so that you can use the shortcode like [list-products categories=”1,2,3″], and then explode them as an array to insert them as your terms? <?php function list_products_func($atts) { $a = shortcode_atts(array( ‘categories’ => ”, ), $atts); // Check for multiple categories by comma if ( strpos( $a[‘categories’], ‘,’ ) !== false … Read more