Easy way to write complex queries in wordpress

I wouldn’t say it’s “easy”, but any complex queries like the one you have that can’t be handled via the API, WP_Query, or its filters should use the wpdb class. global $wpdb; $rows = $wpdb->get_results( ” SELECT p1.meta_value as avg, p2.meta_value as total, p1.meta_value * p2.meta_value as product FROM $wpdb->postmeta as p1, $wpdb->postmeta as p2 … Read more

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

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

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

List of ways to access WordPress database?

There’s some overlap, but each isn’t an abstraction of the previous, and there are definitely some things you don’t want to do outside of the API. Most things, actually! WP_Query is for retrieving posts and pages, it doesn’t update anything, and it doesn’t fetch data that isn’t a post or page. The higher level API … Read more

Set post number to single posts

The answer from the OP inspired me to quickly write this function which is not resource intensive and really fast. I also decided to try another method as described by the OP To accomplish this, I’ve made use of the Transient API and the post_updated action THE LOGIC To get the position of a post, … Read more

Parent and child categories

Have a look at get_the_terms()? <?php // Change ‘category’ if using a custom taxonomy and not referring to the default blog categories $terms = get_the_terms( $post->ID, ‘category’ ); ?> <tr> <td><?php foreach($terms as $term) { // show which categorie(s) under ABC if( $term->parent == ‘0’ ) ) { echo $term->name; } } ?></td> <td><?php foreach($terms … Read more

Pagination is not working with custom query inside a homepage template

I finally made it work. Leaving the answer here in case someone else have the same problem. <div id=”content”> <?php global $paged; global $new_query; $paged = get_query_var(‘page’); query_posts( array( ‘cat’ => 251, ‘paged’ => $paged ) ); $new_query = new WP_Query(); $new_query->query(‘cat=251&posts_per_page=3&paged=’.$paged); ?> <?php while ($new_query->have_posts()) : $new_query->the_post(); ?> <?php the_title(); ?> <?php endwhile; ?> … Read more