Add Snippet To Project
<?php
public function display_books_info_table()
{
// Display the contents of the books_info table.
// Consider using the WP_List_Table class for displaying the table.
global $wpdb;
$table_name = $wpdb->prefix . 'books_info';
$query = "SELECT b.post_id, b.isbn, p.post_title, GROUP_CONCAT(DISTINCT pub.name ORDER BY pub.name ASC SEPARATOR ', ') AS publishers, GROUP_CONCAT(DISTINCT aut.name ORDER BY aut.name ASC SEPARATOR ', ') AS authors
FROM $table_name AS b
INNER JOIN $wpdb->posts AS p ON b.post_id = p.ID
LEFT JOIN $wpdb->term_relationships AS tr ON b.post_id = tr.object_id
LEFT JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
LEFT JOIN $wpdb->terms AS pub ON tt.taxonomy = 'publisher' AND tt.term_id = pub.term_id
LEFT JOIN $wpdb->terms AS aut ON tt.taxonomy = 'authors' AND tt.term_id = aut.term_id
GROUP BY b.post_id";
$results = $wpdb->get_results($query);
echo '<div class="wrap">';
echo '<h1>' . __('Books Info', 'professional') . '</h1>';
if ($results) {
echo '<table class="wp-list-table widefat fixed striped">';
echo '<thead>';
echo '<tr>';
echo '<th>' . __('Post ID', 'professional') . '</th>';
echo '<th>' . __('Title', 'professional') . '</th>';
echo '<th>' . __('ISBN', 'professional') . '</th>';
echo '<th>' . __('Publishers', 'professional') . '</th>';
echo '<th>' . __('Authors', 'professional') . '</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
foreach ($results as $result) {
echo '<tr>';
echo '<td>' . $result->post_id . '</td>';
echo '<td>' . $result->post_title . '</td>';
echo '<td>' . $result->isbn . '</td>';
echo '<td>' . $result->publishers . '</td>';
echo '<td>' . $result->authors . '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
} else {
echo '<p>' . __('No books found.', 'professional') . '</p>';
}
echo '</div>';
}
In this updated code, we are modifying the SQL query to fetch the additional information associated with the book, such as the post title, publisher taxonomies, and author taxonomies. We're using JOIN statements to retrieve the information from the related tables.
Next, we're iterating over the query results to display the information in a table format. The table includes columns for Post ID, Title, ISBN, Publishers, and Authors.
Save the updated code and refresh the Books Info page. You should now see the taxonomies and other options associated with the book displayed in the table.
If you have any further questions or need additional assistance, please let me know.
