2
$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.

1 Answer 1

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.org/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;
1
  • Sorry for the long time to answer but thanks!!! It was very helpful man!
    – Eduardo
    Jan 28, 2015 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.