admin管理员组

文章数量:1331443

$table = $wpdb->prefix . 'posts';
            foreach ( $wpdb->get_col( "DESC " . $table, 0 ) as $column ){//get all columns from wp_posts
                echo "<div><p>".
                $counter."- ".$column." => ".
                "<input class='input_triplify_posts' value='correspondence' id='correspondence".$counter."' mk='".$column."' />".
                "</p></div>";
                $counter++;
            }

Guys, with this code I can get all columns data from the table wp_posts in my database. I just want to get the data from a column named "guid" from the same table. What I have to modify in the code above? Thanks.

$table = $wpdb->prefix . 'posts';
            foreach ( $wpdb->get_col( "DESC " . $table, 0 ) as $column ){//get all columns from wp_posts
                echo "<div><p>".
                $counter."- ".$column." => ".
                "<input class='input_triplify_posts' value='correspondence' id='correspondence".$counter."' mk='".$column."' />".
                "</p></div>";
                $counter++;
            }

Guys, with this code I can get all columns data from the table wp_posts in my database. I just want to get the data from a column named "guid" from the same table. What I have to modify in the code above? Thanks.

Share Improve this question asked Jan 21, 2015 at 20:14 EduardoEduardo 211 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Try this. Hopefully I've interpreted your output correctly.

I'd recommend a good read of the $wpdb codex, particularly in relation to the get_col() method - http://codex.wordpress/Class_Reference/wpdb#SELECT_a_Column

global $wpdb;   // You won't need this unless this code is within a function

$query = $wpdb->prepare(
    'SELECT %1$s.guid FROM %1$s',
    $wpdb->posts
);
$results = $wpdb->get_col($query);
$counter = 1;   // Ignore this if you already set counter somewhere else, or change it as required

if(!empty($results)) : foreach($results as $result) :

        echo '<div><p>';

            printf(
                '%1$s- %2$s =>',
                $counter,
                $result
            );
            printf(
                '<input class="input_triplify_posts" value="correspondence" id="correspondence%1$s mk="%2$s" />',
                $counter,
                $result
            );

        echo '</p></div>';

        $counter++;

    endforeach;
endif;

本文标签: pluginsDoubt using wpbdgtgetcol for a single column