Hook to be used when creating a database table

As mentioned, after_switch_theme should be a pretty good choice. Your table create/update function will only run when you switch themes, which should be roughly like activating a plugin. If you use dbDelta to create and/or update the table there should be no issue with table creation. From the Codex: global $wpdb; $installed_ver = get_option( “jal_db_version” … Read more

How to run this SQL query of wp_terms database

Per comments, you basically have it – just GROUP BY $wpdb->term_relationships.object_id instead; also to be pernickety, you’d use standard (INNER) JOINs rather than LEFT ones: SELECT $wpdb->term_relationships.object_id FROM $wpdb->term_relationships JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id JOIN $wpdb->terms ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id WHERE name IN (394, 396, 988, 666) GROUP BY $wpdb->term_relationships.object_id ORDER BY $wpdb->term_relationships.term_taxonomy_id DESC