Im trying to loop through my custom data in a table to display on the page. Everything is working as intended except the posting to a page using the wp_update_post() function.
Here is my function:
function update_insert_page() {
global $wpdb;
$table_name = $wpdb->base_prefix . 'portalorgs';
$info = $wpdb->get_results( "SELECT * FROM $table_name" );
foreach ($info as $inf) {
$orgname = $inf->name;
$insertpage = array(
'ID' => 224,
'post_content' => $orgname . '<br />'
);
wp_update_post( $insertpage );
}
}
It displays and loops correctly on var_dump
Name: Home
Name: stugotz
Name: tupac
Name: mister2
Name: Okman
Name: MrOk
But on the actual page itself it only posts the last one:
MrOk
I want to be able to display ALL the names from the DB instead of the last one. For reference the update_insert_page() function gets called in a plugin.
Im trying to loop through my custom data in a table to display on the page. Everything is working as intended except the posting to a page using the wp_update_post() function.
Here is my function:
function update_insert_page() {
global $wpdb;
$table_name = $wpdb->base_prefix . 'portalorgs';
$info = $wpdb->get_results( "SELECT * FROM $table_name" );
foreach ($info as $inf) {
$orgname = $inf->name;
$insertpage = array(
'ID' => 224,
'post_content' => $orgname . '<br />'
);
wp_update_post( $insertpage );
}
}
It displays and loops correctly on var_dump
Name: Home
Name: stugotz
Name: tupac
Name: mister2
Name: Okman
Name: MrOk
But on the actual page itself it only posts the last one:
MrOk
I want to be able to display ALL the names from the DB instead of the last one. For reference the update_insert_page() function gets called in a plugin.
Try below code. it will get all data from that table and update page content. you can modify as per your need. let me know if this works for you.i assume that $inf->name is return just names.
function update_insert_page() {
global $wpdb;
$table_name = $wpdb->base_prefix . 'portalorgs';
$info = $wpdb->get_results( "SELECT * FROM $table_name" );
$orgname = '';
if(!empty($info))
{
foreach ($info as $inf) {
$orgname .= 'Name :'.$inf->name.'<br/>';
}
$insertpage = array(
'ID' => 224,
'post_content' => $orgname
);
wp_update_post( $insertpage );
}
}