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

WPDB for post count on post

The problem you are having is that get_queried_object returns results based on the query that was run. On author.php, the queried object was an author. On single.php, page.php, or any custom post type template, the queried object will be the post, not the author. This means that if you want to use $wp_query->get_queried_object();, you will … Read more

wp query for popular post with simple field

The correct query is: $args1 = array( ‘showposts’ => 5, ‘ignore_sticky_posts’ => 1, //ORDER ARGUMENTS ‘meta_key’ => ‘post_views_count’, ‘orderby’ => ‘meta_key_num’, ‘order’ => ‘DESC’, //META QUERY ‘meta_query’ => array( array( ‘key’ => ‘_simple_fields_fieldGroupID_2_fieldID_1_numInSet_0’, ‘compare’ =>’NOT EXISTS’ ), ), ); $popularposts = new WP_Query($args1); The order aguments are always outside meta_query, see the codex.

Trying to retrieve random post, getting a page

You need to specify the post type to be a post – try this for your $args instead: $args = array( ‘post_type’ => ‘post’, // Specifying you want posts only ‘posts_per_page’ => 1, ‘orderby’ => ‘rand’, ‘post_status’ => ‘publish’, ); And most importantly, you need to apply the $args to your query, so do this … Read more