view queries made?

See this codex page. in wp-config.php: define(‘SAVEQUERIES’, true); then in your template: if (current_user_can(‘administrator’)){ global $wpdb; echo “<pre>”; print_r($wpdb->queries); echo “</pre>”; } or without the above SAVEQUERIES, you can still see just the main query: global $wp_query; echo $wp_query->request; or to see all of $wp_query: <pre> <?php print_r($wp_query); ?> </pre>

$wpdb->get_var not returning a result

$wpdb->get_var returns a single variable, but your SQL statement has SELECT * which returns a row of multiple columns. You need to change your SQL statement to SELECT ID … instead of SELECT * …

Retrieve and display data from custom db table in admin area?

Working code for adding widget to wp dashboard with information from custom DB: /** * Add application widget to the dashboard. */ function addApplicationWidget() { wp_add_dashboard_widget( ‘submitted_applications’, ‘Submitted Applications’, ‘showApplicants’ ); } add_action( ‘wp_dashboard_setup’, ‘addApplicationWidget’ ); function showApplicants() { global $wpdb; $appTable = $wpdb->prefix . “applications”; $query = $wpdb->prepare(“SELECT * FROM $appTable WHERE %d >= … Read more

Show All Posts Insert Edit Link

See if this doesn’t do it: First, the callback for that Ajax search (the wp_link_query method in wp-includes/class-wp-editor.php) suppresses the normal filters. We have to turn them back on for this particular query. function undo_suppress($qry) { global $_POST; if (isset($_POST[‘action’]) && ‘wp-link-ajax’ == $_POST[‘action’]) { $qry->set(‘suppress_filters’,false); } } add_action(‘pre_get_posts’,’undo_suppress’); Now we can use the posts_where … Read more

Order post by year DESC and month ASC

To show a list by Year DESC and by month ASC: change your post type with custom_post. global $wpdb; $posts = $wpdb->posts; $sql = “SELECT DISTINCT(YEAR(`post_date`)) as years FROM $posts WHERE post_type=”custom_post” ORDER BY years DESC”; //Get all post year list by DESC $result = $wpdb->get_results($sql); foreach($result as $rs) { echo ‘<h2>’.$rs->years.'</h2>’; $args = array( … Read more

How to tell if $query_var isset?

use the indentical comparison operator: if(get_query_var(‘cls’) !== ” && get_query_var(‘ch’) !== ”) { mcs_textbook_chapter($dialect, $cls, $ch); } elseif(get_query_var(‘cls’) !== ”) { mcs_textbook_chapter($dialect, $cls); } else { mcs_textbook($dialect); }

post loop causes wp_users and wp_usermeta DB queries for each users

Whilst WordPress has gotten pretty good at db caching for loops (thumbnails, post meta, terms), it does not pre-emptively cache users (authors). So if you have 5 different authors across all the posts in your loop, that’s 10 queries (1 for the user object, 1 for the user’s meta cache stash). It’s thanks to WP_Query::setup_postdata() … Read more