get_query_var function not working at all

WordPress doesn’t automatically add all query string params ($_GET params) as query_vars. When Query Var somevar is not registered: example.com/some-page/?somevar=hello – WordPress ignores somevar When Query Var somevar is registered: example.com/some-page/?somevar=hello – WordPress stores the value of this param in the $wp_query->query_vars array The difference between registering that variable with WordPress is whether the value … Read more

WP_Query: get 3 random posts from 10 latest

There’s one way with: $args = [ ‘post_type’ => ‘post’, ‘posts_per_page’ => 10, ‘orderby’ => ‘date’, ‘order’ => ‘DESC’, ‘no_found_rows’ => ‘true’, ‘_shuffle_and_pick’ => 3 // <– our custom argument ]; $query = new \WP_Query( $args ); where the custom _shuffle_and_pick attribute is supported by this demo plugin: <?php /** * Plugin Name: Support for … Read more

Exclude or Include category ids in WP_Query

As you probably know it, categories are taxonomies. When you use the arguments such as category__in, it will add a tax query to your WP_Query(). So, your situation would be something like this: $args = array( ‘post_type’ => ‘post’, ‘tax_query’ => array( ‘relation’ => ‘AND’, array( ‘taxonomy’ => ‘category’, ‘field’ => ‘term_id’, ‘terms’ => array( … Read more

Order by DESC, ASC in custom WP_Query

Try this: $args = array( ‘post_type’ => ‘post’, ‘meta_key’ => ‘pb_issue_featured’, ‘orderby’ => ‘meta_value’, ‘order’ => ‘DESC’, ‘posts_per_page’ => $posts, ‘paged’ => $paged, ‘paged’ => 1, ‘meta_query’ => array( array( ‘key’ => ‘headline’, ‘value’ => 1, ‘compare’ => ‘!=’ ) ) ); add_filter( ‘posts_orderby’, ‘filter_query’ ); $q = new WP_Query($args); remove_filter( ‘posts_orderby’, ‘filter_query’ ); function … Read more

Loop within a loop?

You can create any number of loops as you wish by creating more WP_Query objects $query = new WP_Query($args); while ($query->have_posts()) : // initialization for $inner_args & backup the current global $post $inner_query = new WP_Query($inner_args); while ($inner_query->have_posts()) : // do something endwhile; // restore the global $post from the previously created backup endwhile; EXPLANATION … Read more

Sorting: custom query with orderby meta_value_num THEN by title

This is very crude but should sort your posts by year (meta_value) and then by title. It does depend on how the query is setup so it will only work with the query below or with similar ones. function alter_order_wpse_103181($order,$qry) { remove_filter(‘posts_orderby’,’alter_order’,1,2); $order = explode(‘,’,$order); $order = implode( ‘ ASC,’,$order); return $order; } add_filter(‘posts_orderby’,’alter_order_wpse_103181′,1,2); $q … Read more

How can I use order_by to order by two meta_keys without excluding posts that don’t have those keys initialized?

I had a similar issue but couldn’t solved with the snippets on this thread. I had to order a query by: all ‘featured’ posts first (is_it_a_featured_etf) and by a numeric field (etf_aum) in DESC order after the featured ones. My solution: ‘meta_query’ => [ ‘relation’ => ‘OR’, ‘etf_aum’ => array( ‘key’ => ‘etf_aum’, ‘type’ => … Read more

get_queried_object() returns null on post date archive

so my question is, is this intended functionality (e.g. are you not supposed to use get_queried_object() on those pages), a technical limitation (is there not a meaningful object to return on those pages), or simply an implementation oversight? get_queried_object() is to get the term, author, single post, single custom post type, or page object being … Read more

Set Alias for meta_query arguments in get_posts()

Have a look at the meta_query_find_compatible_table_alias filter defined in wp-includes/class-wp-meta-query.php. This filter’s documentation: /** * Filters the table alias identified as compatible with the current clause. * * @since 4.1.0 * * @param string|bool $alias Table alias, or false if none was found. * @param array $clause First-order query clause. * @param array $parent_query Parent … Read more