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

How to Display Posts on a Tag Archive Page Based on Custom Field(term meta) Values in WordPress?

Use this- function custom_tag_archive_query( $query ) { if ( is_tag() && $query->is_main_query() && !is_admin() ) { $tag = get_queried_object(); $tag_name = $tag->name; $similar_names = get_term_meta( $tag->term_id, ‘similar_names’, true ); $similar_names_array = explode( ‘,’, $similar_names ); $similar_names_array = array_map( ‘trim’, $similar_names_array ); $similar_names_array[] = $tag_name; // include the tag name itself in the search terms $meta_query … Read more

How can I incldue a “private” post type in a loop for public users?

Figured it out, it was simpler than I expected. /** * Include catalog posts in archives, even though they’re not “public” * * @param WP_Query $query * @return WP_Query */ function wpse415668_catalog_posts_in_archives(WP_Query $query): WP_Query { if (! $query->is_admin() && $query->is_main_query() && $query->is_tax(“catalog_category”)) { $query->set(“post_type”, “catalog”); } return $query; } add_filter(“pre_get_posts”, “wpse415668_catalog_posts_in_archives”);

techhipbettruvabetnorabahisbahis forumutaraftarium24edusedusedusedueduseduseduseduedusedus