Why execute the_post()?

It is necessary to execute that function if you want to use the template functions such as the_title() & the_permalink(). You should not use it if you aren’t using those kind of functions but for the themes it’s generally recommended to use these functions since they automatically ensure that all wordpress filters are being applied. … Read more

Custom post query by taxonomy

I’m slightly surprised that the first would work, because it’s terms, not term. Anyway, the correct way to do this is: $args = array( ‘post_type’ => array(‘product’, ‘comic’, ‘magazine’), ‘tax_query’ => array( array( ‘taxonomy’ => ‘Genres’, ‘terms’ => array( ‘hot’, ‘home’ ), ), ‘posts_per_page’ => 10 );

WP_Query orderby breaks when using AJAX?

I believe Milo is right, it’s probably a filter affecting this. Ajax requests look like admin requests (that is, is_admin() returns true), so it’s possible that something is checking if is_admin() and adding the filter in those cases. You can set ‘suppress_filters’ => true in your args array and it will probably work. Note that … Read more

How to detect if a function has been fired on any page so scripts/styles can be loaded conditionally

For scripts this is no-brainer – just enqueue inside my_gallery() function and they will be output in footer (since header already passed). However stylesheets are a challenge, they are only supposed to work in head section and enqueueing for footer is not supported. Essentially you need to know if function was called before it was … Read more

Explanation of WP_Query

You are using a WP_Query object, you just don’t know it (yet). WordPress generates a global called $wp_query which is WP_Query object. This object is what is responsible for what is called the “main Loop” colloquially. This $wp_query object is present on every page that I am aware of. Functions like the_post, have_posts, and some … Read more

Only display authors who have posts

The default version of the WordPress function provides the count of the default post type ‘POST’ as stated below: <?php $post_count = count_user_posts($userid); ?> For retrieving the post count based on a post type a custom function as below is required: <?php function count_user_posts_by_type( $userid, $post_type=”post” ) { global $wpdb; $where = get_posts_by_author_sql( $post_type, true, … Read more