Using is_main_query to select custom post type on certain page

The is_home() conditional returns true when the currently displayed page is the blog posts index. If you want to target the site front page specifically instead, you need to use is_front_page(): function wpse83754_filter_pre_get_posts( $query ) { if ( $query->is_main_query() && is_front_page() ) { $query->set( ‘post_type’, array( ‘home_portfolio’ ) ); } } add_action( ‘pre_get_posts’, ‘wpse83754_filter_pre_get_posts’ ); … Read more

Perform query with meta_value date

I did something similar, the technique you need to use is called a meta query. Here is the query I wrote to get posts based on a date value stored as a custom field meta value. query_posts( array( ‘post_type’=>’post’, ‘order’=>’ASC’, ‘orderby’=>’meta_value_num’, ‘meta_key’=>’date_event’, ‘posts_per_page’=> -1, ‘meta_query’ => array( ‘relation’ => ‘AND’, array( ‘key’ => ‘date_event’, ‘value’ … Read more

Query posts distinct authors

With what you are asking, it is really difficult to come up with some performant easy way that is also reliable. As @birgire already stated, his solution is not reliable, but from tests, it seem to be the fastest clocking in at 2 db queries in about 0.015s average. From a quick discussion between @birgire … Read more

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)