WP_Query Meta_key is text value and need to sort as numeric not working

A meta_value field is always text; the fieldtype in the database structure is longtext, so unless you’re explicitly typecasting when retrieving it (i.e. $price = (float)$custom[‘price’][0]; – which won’t help with ordering in the first place), you can’t ‘adjust’ the custom field data – it’s always stored as text. Having said that, you should simply … Read more

How to overwrite orderby with add_query_var

Use pre_get_posts instead.. function my_archive_query_override( $query ) { if ( $query->is_archive() && $query->is_main_query() ) { $query->set( ‘orderby’, ‘rand’ ); } } add_action( ‘pre_get_posts’, ‘my_archive_query_override’ ); For that user selectable part, you can use AJAX to accomplish that.

Orderby custom meta returns random order

I also added this here for someone else having same issue. If your meta values are numeric then you should use orderby meta_value_num instead of meta_value. As mentioned in codex. meta_value – Note that a meta_key=keyname must also be present in the query. Note also that the sorting will be alphabetical which is fine for … Read more

Do not show children of a category

Just use the parameter category__in of WP_Query – if you do this, then there is no need for cat and category__not_in. category__in does give you back results from the categor(y/ies) specified, but does not include results for the children of the specified categor(y/ies).

Get posts with multiple categories

category__and needs to be an array of category ID’s category__and (array) – use category id. You can modify your code to look like this $query = new WP_Query( array(‘category__and’ => array(12,32)));

WP_Query freezing

From the discussion in comments to your question, here is what I suggest and recommend Just to recap and to add everything in a constructive answer The tag parameter in WP_Query accepts the term slug, not the name Always reset your custom WP_Query, always. Make use of wp_reset_postdata() after your query is done, just before … Read more