0

I need help to get thumbnails in a WordPress database with pure PHP because a need insert this in a Magento Application (Feed). Can someone help me?

I need this:

http://luckifas.nextmp.net/wp/wp-content/uploads/2015/09/fashionista_web_header_by_lstar92-d3gkahi-263x130.jpg

or part of this, example:

  • /uploads/2015/09/fashionista_web_header_by_lstar92-d3gkahi-263x130.jpg
  • /2015/09/fashionista_web_header_by_lstar92-d3gkahi-263x130.jpg

More details:

add_image_size('magento-image', 263, 130, true);

PHP Script used:

function SimpleLoopWordpress (){

    try {

        // PDO em ação!
        $pdo = new PDO ( "mysql:host=localhost;dbname=", "", "", array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") );
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $sql = "SELECT * FROM wp_posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date ASC";
        $stmt = $pdo->prepare($sql);
        $stmt->execute();

        $i = 1;
        echo '<div class="row">';

        while ( $linha = $stmt->fetch ( PDO::FETCH_OBJ ) ) {

            ?>
                <div class="col-md-6">
                    <?php 
                         echo $i++;
                         echo FEATURED IMAGE <--------
                         echo $linha->post_title;
                         echo $linha->post_date;
                         echo $linha->post_content;
                    ?>  
                </div>

            <?php

        }

        echo '</div>';

    }

I posted completly updated php script in pastebin:

http://pastebin.com/49Mbj484

  • So far this point it's ok ---> $meta->execute( $linha->thumbnail_id ); // ids 15 - 16

Thanks

  • 2
    Do you need the file path or URL, or both? – TheDeadMedic Sep 24 '15 at 12:15
  • 2
    Do you have the post ID to work with in your Magento app? – TheDeadMedic Sep 24 '15 at 12:28
  • I have a loop for get others infos but not FEATURED IMAGE. I posted the script. – Marcos Vinicius Sep 24 '15 at 12:38
  • I have updated question and posted complete script in pastebin. I'm not php erros and I'm not image :( – Marcos Vinicius Sep 24 '15 at 14:11
  • Can you help me out then by 1) ensuring PHP error reporting is on 2) debugging some variables with print_r – TheDeadMedic Sep 24 '15 at 14:20
1

Okay, a couple of things - first I would suggest modifying your main query to grab the thumbnail ID at the same time (I have also changed it to only get posts that have a thumb):

$sql = "SELECT wp_posts.*, meta_value AS thumbnail_id FROM wp_posts " .
       "LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id " .
       "WHERE post_type = 'post' AND meta_key = '_thumbnail_id' AND meta_value != '' " .
       "ORDER BY post_date ASC";

Now for the processing:

while ( $linha = $stmt->fetch ( PDO::FETCH_OBJ ) ) {

    $meta = $pdo->prepare( 'SELECT meta_value FROM wp_postmeta WHERE meta_key = "_wp_attachment_metadata" AND post_id = ?' );
    $meta->execute( array( $linha->thumbnail_id ) );

    $file = null; // Failsafe in case anything below fails
    if ( $data = $meta->fetchColumn() ) {
        if ( $data =@ unserialize( $data ) ) {
            $file = $data['file'];

            if ( ! empty( $data['sizes'] ) ) {
                $sizes = $data['sizes'];

                if ( ! empty( $sizes['magento-image'] ) )
                    $file = $sizes['magento-image']['file'];
                elseif ( ! empty( $sizes['thumbnail'] ) ) // You might want to fallback, up to you
                    $file = $sizes['thumbnail']['file'];
            }

            if ( $file !== $data['file'] ) {
                // Files stored in sizes are only the basename
                // If you use year/month uploads, we need to grab that path

                if ( '.' !== $path = dirname( $data['file'] ) )
                    $file = "$path/$file";
            }
        }
    }

    if ( $file ) {
        // Gotcha!
        echo "<img src='http://example.com/wp-content/uploads/$file' />";
    }

}

This code is not tested, so see how you go and report back with any errors.

  • if ( $data = $meta->fetchColumn() ) { <- problem here ... – Marcos Vinicius Sep 24 '15 at 14:37
  • This if return false ... sorry my english :) – Marcos Vinicius Sep 24 '15 at 14:39
  • Meta execute line was wrong, argument should be an array (see revision) – TheDeadMedic Sep 24 '15 at 14:50
  • var_dump($meta->fetchColumn()); return bool(false) bool(false) – Marcos Vinicius Sep 24 '15 at 14:50
  • Owwwwwwwwwwwwwwww you are very very the best programmer man I encountered in this community. Very very very good man !!!! – Marcos Vinicius Sep 24 '15 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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