I'm looking for some help building the base code to query my WP database, retrieve a table and then post this on a page. There seems to be a lot on the web to assist with this, however with limited php/sql coding background I'm finding it hard to wade through everything. I find things easier to learn by reverse engineering and building from there.
I've downloaded the plugin PHP code snippets (Insert PHP) which gives me the ability to add php code into posts (being deprecated in next version) or shortcodes.
I believe the following code should be sufficient to query the wp_post table and return the table data in an array.
global $wpdb;
$result = $wpdb->get_results('SELECT * FROM ' . $wpdb->posts . ' LIMIT 10');
What I want to be able to do from here is generate a html table in a WP page using this data. How would I go about this?
Once I've got this base code sorted I can build off this and develop what I need.
Any help is greatly appreciated.
I'm looking for some help building the base code to query my WP database, retrieve a table and then post this on a page. There seems to be a lot on the web to assist with this, however with limited php/sql coding background I'm finding it hard to wade through everything. I find things easier to learn by reverse engineering and building from there.
I've downloaded the plugin PHP code snippets (Insert PHP) which gives me the ability to add php code into posts (being deprecated in next version) or shortcodes.
I believe the following code should be sufficient to query the wp_post table and return the table data in an array.
global $wpdb;
$result = $wpdb->get_results('SELECT * FROM ' . $wpdb->posts . ' LIMIT 10');
What I want to be able to do from here is generate a html table in a WP page using this data. How would I go about this?
Once I've got this base code sorted I can build off this and develop what I need.
Any help is greatly appreciated.
Share Improve this question asked Jul 14, 2018 at 6:01 Morts81Morts81 1231 silver badge4 bronze badges1 Answer
Reset to default -1$html= "<table>";
foreach($result as $r){
$html .="<tr>";
$html .="<td>".$r->post_title."</td>";
$html .="<td><a href='".get_the_permalink($r->ID)"'>".$r->post_title."</a></td>";
$html .="</tr>";
}
$html .="</table>";
echo $html;
To avoid the depreacated usage of PHP code snippets you can wrap it in a shortcode and use anywhere in a page/post content editor
function Stack_308511_post_grid( $atts ) {
$atts = shortcode_atts( array(
'limit' => 10,
), $atts,'limits' );
global $wpdb;
$html = "";
$result = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix.'posts LIMIT '.$atts['limit']);
$count = $wpdb->num_rows;
if($count >0){
$html .="<table>";
foreach($result as $r){
$html .="<tr>";
$html .="<td>".$r->post_title."</td>";
$html .="<td><a href='".get_the_permalink($r->ID)."'>".$r->post_title."</a></td>";
$html .="</tr>";
}
$html .="</table>";
}
return $html;
}
add_shortcode( 'postGrid', 'Stack_308511_post_grid' );
So in the html editor you can use
- [postGrid] which will use the default 10 post limit or i.e.
- [postGrid limit=20] which will pick the latest 20 posts