Skip to content

Instantly share code, notes, and snippets.

@imvaskii
Created June 25, 2018 05:37
Show Gist options
  • Save imvaskii/2a164678dec86201f81dddaff66c080b to your computer and use it in GitHub Desktop.
Save imvaskii/2a164678dec86201f81dddaff66c080b to your computer and use it in GitHub Desktop.
An example of simple wpdb query script.
<?php
/**
* Returns max ID of the source wp_posts table.
*
* @return int
*/
public function get_source_max_article_id() {
$in_source_post_statuses = [ 'publish', 'draft', 'pending', 'private', 'trash', 'inherit' ];
$post_status_placeholders = implode( ', ', array_fill( 0, count( $in_source_post_statuses ), '%s' ) );
$prepare_values = array_merge( [ $this->post_type ], $in_source_post_statuses );
$source_db = self::$source_db_handle;
$sql = $source_db->prepare(
"SELECT max(ID) FROM {$source_db->prefix}posts
WHERE post_type=%s AND post_status IN ( {$post_status_placeholders} )",
$prepare_values
);
return (int) $source_db->get_var( $sql );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment