How to change posts order on Category and Tag archives?

You can use pre_get_posts action to modify this order. Here’s the code: function my_change_posts_order( $query ){ if ( ! is_admin() && ( is_category() || is_tag() ) && $query->is_main_query() ) { $query->set( ‘order’, ‘ASC’ ); } }; add_action( ‘pre_get_posts’, ‘my_change_posts_order’); What we do here is: We’re adding our function that will modify params for WP_Queries We … Read more

How to display posts month by month?

WordPress 3.7 introduced the date_query to display posts month by month: $args = array( ‘date_query’ => array( array( ‘month’ => $month ) ) ); $query = new WP_Query( $args ); Note : $month refers to month number (1-12)

get_query_var returns null

You have to add any query vars that are not WordPress objects to the array of recognized query vars to be able to retrieve it from the $wp_query global: add_filter( ‘query_vars’, ‘wpa66452_query_vars’ ); function wpa66452_query_vars( $query_vars ){ $query_vars[] = ‘tree’; return $query_vars; }

How can I get an array of all IDs from the current queried object?

You are overwriting $post_ids variable on every while loop, never collecting them. That can be solved using $post_ids = array(); while (have_posts()) : the_post(); $post_ids[] = get_the_ID(); endwhile; var_dump($post_ids); // this is an array of ids However there is simpler way, you can skip the whle cycle and simply run: if( function_exists( ‘wpseo_local_show_map’ ) && … Read more

Use ‘parse_query’ filter to show posts that from multiple criteria

Ignore my comment about meta_query. Not only does it not work with $query->set(), you wouldn’t be able to control the critical “A OR B” requirement of the query. Instead, I believe what you require is possible via a combination of both the pre_get_posts action hook and posts_where filter hook as follows. add_action(‘pre_get_posts’, ‘my_show_appropriate_posts’); function my_show_appropriate_posts($query){ … Read more

Detect featured image among the attached images

Simply add an post__not_in argument and use the get_post_thumbnail_id() function. $args = array( ‘post_type’ => ‘attachment’, ‘post_parent’ => $product_id, ‘post_mime_type’ => ‘image’, ‘orderby’ => ‘menu_order’, ‘order’ => ‘ASC’, ‘numberposts’ => -1, ‘post__not_in’ => array(get_post_thumbnail_id($product_id)) ); $attachments = get_posts($args);