Query WordPress Database and Post HTML Table

$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