$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>sql - Query WordPress Database and Post HTML Table|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

sql - Query WordPress Database and Post HTML Table

matteradmin11PV0评论

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 badges
Add a comment  | 

1 Answer 1

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
Post a comment

comment list (0)

  1. No comments so far