Skip to content

Instantly share code, notes, and snippets.

@richaber
Created July 8, 2022 19:48
Show Gist options
  • Save richaber/130a2aaed9363bd5968bb80fe7e03158 to your computer and use it in GitHub Desktop.
Save richaber/130a2aaed9363bd5968bb80fe7e03158 to your computer and use it in GitHub Desktop.
WPDB, get_row for a post with the given post_title of the given post_type (uncached, just for analysis)
global $wpdb;

$query = "
SELECT *
FROM   $wpdb->posts
WHERE  $wpdb->posts.post_title=%s
AND    $wpdb->posts.post_type=%s
LIMIT  1
";

// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching
$row = $wpdb->get_row(
    $wpdb->prepare(
        $query,
        $term->name,
        POST_TYPE::SLUG
    )
);
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching

// the var_dump would be an Object with the following structure...
$row = (object) [
    'ID' => '1',
    'post_author' => '1',
    'post_date' => '2022-07-08 12:24:27',
    'post_date_gmt' => '2022-07-08 18:24:27',
    'post_content' => '',
    'post_title' => 'Hello World',
    'post_excerpt' => '',
    'post_status' => 'publish',
    'comment_status' => 'closed',
    'ping_status' => 'closed',
    'post_password' => '',
    'post_name' => 'hello-world',
    'to_ping' => '',
    'pinged' => '',
    'post_modified' => '2022-07-08 12:24:27',
    'post_modified_gmt' => '2022-07-08 18:24:27',
    'post_content_filtered' => '',
    'post_parent' => '0',
    'guid' => 'https://example.local/?p=1',
    'menu_order' => '0',
    'post_type' => 'post',
    'post_mime_type' => '',
    'comment_count' => '0',
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment