wp query template tags not working

Template tags are not methods of WP_Query object. They are functions. On the other hand have_posts and the_post are methods of WP_Query. So in your code you should use: while ($test->have_posts()) : $test->the_post(); as you do, but then: echo get_the_title(); echo get_the_content(); Also… if you want to echo these values, it would be much better … Read more

How to get the WC_Product Object when using a WP_Query in WooCommerce

The global $product; doesn’t work in a custom WP_Query, instead use wc_get_product() like: if ($slider_products_q->have_posts()): while($slider_products_q->have_posts()): $slider_products_q->the_post(); $product_id = get_the_ID(); $product = wc_get_product($product_id); $price_html = $product->get_price_html() // … Now you can use any WC_Product methods on $product variable. Alternatively, you could use a WC_Product_query.

Exclude admin from WP_Query Contributors

I realize now that you’re not talking about WP_Query but WP_User_Query. Looking at the documentation for WP_User_Query it says this about the who parameter: who (string) – Which users to query. Currently only ‘authors’ is supported. Default is all users. Instead you can use the role parameter and exclude certain users by ID like so: … Read more

WP_Query with Metavalue

I would recommand to add meta_query in array param, try this add_shortcode( ‘sc_count_brands’, ‘sc_count_brands_code’ ); function sc_count_brands_code($atts) { $values = shortcode_atts( array(‘category’ => ‘Horology’,), $atts ); $args = array( ‘post_type’ => ‘brands’, ‘post_status’ => ‘publish’, ‘meta_query’ => array( array( ‘key’ => ‘br_category’, ‘value’ => esc_attr($values[‘category’]), ‘compare’ => ‘=’ ), ), ); $query = new WP_Query( … Read more

Order a query result by a numeric meta key even if it does not exist, but put the posts with meta key first

First of all, just to clarify, in a normal situation without the specific order requirements, and assuming you are doing this on pre_get_posts hook, you would just do as follows: add_action(‘pre_get_posts’, function ($query) { // only perform for a given query // do your checks and return early if (! $query->is_main_query() || ! $query->is_home()) { … Read more