Get an array of meta_values for a user meta_key

You just need to add a DISTINCT to your SQL query, something like: $cities = $wpdb->get_col(“SELECT DISTINCT(meta_value) FROM $wpdb->usermeta WHERE meta_key = ‘my_cities_meta_key'” ); Alternatively, if you want to do it with php for some reason (if you want to know that a city is listed twice before displaying only unique entries) $cities = $wpdb->get_col(“SELECT … Read more

new WP_Query to get max price meta value not working

As @Milo suggested and also codex documented it is_main_query Under_the_Hood This function is an alias for the method WP_Query::is_main_query(). In filter or action hook callbacks that are passed the WP_Query object, such as pre_get_posts, it is circular to call this function. Instead, directly call the passed object’s method. For example, if your filter callback assigns … Read more

Need wp_query to return all children and grandchildren

Ok, I found a solution. I first looped through and got all the child page ID’s, created an array, and used post_parent__in <?php $parentarray = array($parentid); $parentposts = new WP_Query( array( ‘posts_per_page’ => -1, ‘post_type’ => ‘page’, ‘post_parent’ => $parentid ) ); while ( $parentposts->have_posts() ): $parentposts->the_post(); ?> <?php $parentarray[] = get_the_id(); ?> <?php endwhile; … Read more

Custom query for sidebar isn’t returning results

Based on our chat in the comments here’s what I came with. Please advise if it works. <?php function primary_category_query_shortcode( $post, $atts ) { //global $post; //Uncomment this global if it still doesn’t work. $atts = shortcode_atts( $defaults, $atts, ‘primary_category_query’ ); $my_meta = get_post_meta ( $post->ID, ‘_yoast_wpseo_primary_category’, true ); // WP_Query arguments $args = array … Read more

Query post order by post and desc not working

post is not a valid value for orderby parameter neither DESC. You can choose any of these values. Also, you should stop using query_post. <?php $args = array( ‘post_type’ => ‘ranking’, ‘orderby’ => ‘menu_order’, ‘order’ => ‘DESC’, ‘posts_per_page’ => 10, ‘meta_key’ => ‘top10’, ‘meta_value’ => ‘sim’ ); $loop = new WP_Query( $args ); while ( … Read more