On the 'Edit Profile' page I'm trying to loop through all the category names using $wpdb. Here is my code
<?php
function custom_user_profile_fields($profileuser) {
?>
<h1>Select a Category</h1>
<select name="category">
<?php
global $wpdb;
$terms = $wpdb->query( "SELECT name FROM wp_terms" );
foreach ( $terms as $term ) { ?>
<option value=""><?php echo $term; ?></option>
<?php }
?>
</select>
<?php
}
add_action('show_user_profile', 'custom_user_profile_fields');
add_action('edit_user_profile', 'custom_user_profile_fields');
But I'm getting empty option filed.
On the 'Edit Profile' page I'm trying to loop through all the category names using $wpdb. Here is my code
<?php
function custom_user_profile_fields($profileuser) {
?>
<h1>Select a Category</h1>
<select name="category">
<?php
global $wpdb;
$terms = $wpdb->query( "SELECT name FROM wp_terms" );
foreach ( $terms as $term ) { ?>
<option value=""><?php echo $term; ?></option>
<?php }
?>
</select>
<?php
}
add_action('show_user_profile', 'custom_user_profile_fields');
add_action('edit_user_profile', 'custom_user_profile_fields');
But I'm getting empty option filed.
Share Improve this question asked Feb 16, 2019 at 10:41 ShihabShihab 1341 gold badge3 silver badges11 bronze badges 8 | Show 3 more comments1 Answer
Reset to default 1The $wpdb->query()
method doesn't return query results but a count on how many rows were affected by the query. To get the results you have to user the $wpdb->get_results($sqlString)
method and then iterate over it.
<?php
function custom_user_profile_fields($profileuser) {
?>
<h1>Select a Category</h1>
<select name="category">
<?php
global $wpdb;
$terms = $wpdb->get_results( "SELECT name FROM wp_terms" );
foreach ( $terms as $term ) { ?>
<option value=""><?php echo $term->name; ?></option>
<?php }
?>
</select>
<?php
}
add_action('show_user_profile', 'custom_user_profile_fields');
add_action('edit_user_profile', 'custom_user_profile_fields');
get_terms
but I really need this using $wpdb for a specific reason. – Shihab Commented Feb 16, 2019 at 11:16