When to use WP_query(), query_posts() and pre_get_posts

You are right to say: Never use query_posts anymore pre_get_posts pre_get_posts is a filter, for altering any query. It is most often used to alter only the ‘main query’: add_action(‘pre_get_posts’,’wpse50761_alter_query’); function wpse50761_alter_query($query){ if( $query->is_main_query() ){ //Do something to main query } } (I would also check that is_admin() returns false – though this may be … Read more

Modify WordPress Search

You could try something like this below. Basically, if we are performing a search and it is the main_query (the query that generates the page results) ignore the default search and perform an exact match via the query_where function custom_product_search_exact_match( $query ) { global $wpdb; if ( is_search() && is_main_query() && !empty( $query->query_vars[‘s’] ) ) … Read more

get_the_ID() retrieves same ID on Gutenberg’s Query Loop

No, you did nothing wrong. It’s a known issue and in WordPress v6.1.3 and up to the current stable release as of writing (v6.2.2), it’s happening because of the following lines in get_the_block_template_html() which returns the markup for the current (block-based) template: $content = do_shortcode( $content ); $content = do_blocks( $content ); So as you … Read more

new WP_Query with order args – no more distinction between categories

On the template driving your archive page, you can use the query_posts() function to adjust the main query (WordPress’ default main query, not your custom one). query_posts( array( ‘orderby’ => ‘modified’, ‘order’ => ‘ASC’, ) ); Using the pre_get_posts action is the better method, though slightly more complex (untested, would go in theme’s functions.php or … Read more

How do I exclude the current post from the upcoming post query

I think you are asking how to exclude the current exhibition from your upcoming exhibitions query. If that is the case, simply insert the ID of the current exhibition into the query using ‘post__not_in’: $exhibition_upcoming_query = array( ‘post_type’ => ‘exhibition’, ‘orderby’ => ‘meta_value’, ‘order’ => ‘ASC’, ‘posts_per_page’ => 3, ‘post__not_in’ => array( $exhibition_current_loop->posts[0]->ID ), ‘meta_query’ … Read more

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