SQL Query inside Widget

“SELECT name FROM wp_venues WHERE name=””.the_field(“venue’).”‘ “; will only return the value of the_field(‘venue’) which is already known. “SELECT * FROM wp_venues WHERE name=””.the_field(“venue’).”‘ “; will select the whole row with all fields. Try too use $wpdb->prepare for security reasons. With $wpdb->get_row you get the whole row as either an object, a numerically indexed array … Read more

WP_Query not retrieving by random order under parent

Turns out the “Custom Page Order” plugin was causing the randomization error… or rather, not allowing any randomization as it overrides it completely. Disabled the plug-in. Whenever I need to change orders, I can enable the plugin, shuffle pages around, and then disable. But the need for this is decreasing more and more as we … Read more

Count total number of post in foreach loop

Edit: Re-reading your question I noticed I may have mixed some things up, but the point remains that you should combine these separate queries performed inside the loop, by using variables inside the loop to create a comprehensive list of the query arguments that you can use to perform one query outside the loop. I’ve … Read more

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

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