GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   PHP question (https://gfy.com/showthread.php?t=1114181)

BSleazy 07-01-2013 08:47 PM

PHP question
 
I'm trying to change some code to get ONLY the featured images from the media library in wordpress. I'm losing my mind, any ideas? Here's some of the code.

Code:

{
       
        $homepage_items = -1;

        $args = array(
            'post_type' => 'attachment',
            'numberposts' => $homepage_items,
            'post_status' => null,
            'post_parent' => $post->ID,
            'order' => 'ASC',
            'orderby' => 'menu_order',
        );
        $all_photo_arr = get_posts( $args );
}


vdbucks 07-01-2013 08:56 PM

Quote:

Originally Posted by BCyber (Post 19695608)
I'm trying to change some code to get ONLY the featured images from the media library in wordpress. I'm losing my mind, any ideas? Here's some of the code.

Code:

{
       
        $homepage_items = -1;

        $args = array(
            'post_type' => 'attachment',
            'numberposts' => $homepage_items,
            'post_status' => null,
            'post_parent' => $post->ID,
            'order' => 'ASC',
            'orderby' => 'menu_order',
            'meta_key' => '_thumbnail_id'
        );
        $all_photo_arr = get_posts( $args );
}

You could also then sort them by meta_value if you so desire using "'orderby' => 'meta_value'" and then set "'order' => 'ASC|DESC'" (either or, not both)

The above of course assuming that you only want posts with a featured image... if not then http://codex.wordpress.org/Function_...post_thumbnail

Colmike9 07-01-2013 08:58 PM

This should get the URL of only the featured image:

<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<stuff><img src='<?php echo $image[0]; ?>'/></stuff>

<?php endif; ?>

Edit: Might not be what you're looking for. Look at the reply above. :)

BSleazy 07-01-2013 09:19 PM

Quote:

Originally Posted by Colmike7 (Post 19695614)
This should get the URL of only the featured image:

<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<stuff><img src='<?php echo $image[0]; ?>'/></stuff>

<?php endif; ?>

Edit: Might not be what you're looking for. Look at the reply above. :)

Ya I know how to do all that but this is different. It's part of a function and I need to get all of the featured images from the media library. Displaying them individually isn't a problem.

vdbucks 07-01-2013 09:46 PM

Quote:

Originally Posted by BCyber (Post 19695637)
Ya I know how to do all that but this is different. It's part of a function and I need to get all of the featured images from the media library. Displaying them individually isn't a problem.

See my post above... noting specifically the "'meta_key' => '_thumbnail_id'" in the query args...

If, however, you want to discard all other post data except for the featured image info, then you'll have to do a more advanced query with wpdb, join some tables and such... I could give you an example if this is what you want to do, but to be honest, the savings in overhead won't be that great compared to just running a normal query like I gave you above.

MainstreamGuy 07-01-2013 09:52 PM

I'm always surprised by the amount of people on GFY that are PHP experts.

It's like the 2013 average webmaster knows about everything, programming, marketing, design, blogging, tubes, etc etc...

They are true geniuses when you compare them with webmasters from year 2005.

BSleazy 07-01-2013 09:55 PM

Quote:

Originally Posted by vdbucks (Post 19695660)
See my post above... noting specifically the "'meta_key' => '_thumbnail_id'" in the query args...

If, however, you want to discard all other post data except for the featured image info, then you'll have to do a more advanced query with wpdb, join some tables and such... I could give you an example if this is what you want to do, but to be honest, the savings in overhead won't be that great compared to just running a normal query like I gave you above.

I did try your example about but all the posts/thumbs disappeared so I don't know what the problem is.

vdbucks 07-01-2013 10:12 PM

Quote:

Originally Posted by BCyber (Post 19695672)
I did try your example about but all the posts/thumbs disappeared so I don't know what the problem is.

Try changing "$all_photo_arr = get_posts( $args );" to "$all_photo_arr = new WP_Query($args);"

The following works as intended for me:

Code:

$args  = array(
                      'post_type' => 'post',
                      'meta_key' => '_thumbnail_id',
                      'posts_per_page' => 50,
                      'paged' => $paged
                    );
$all_photo_arr = new WP_Query($args);

if ( $all_photo_arr->have_posts() ) :
  while ( $all_photo_arr->have_posts() ) : $all_photo_arr->the_post();
    the_post_thumbnail();
  endwhile;
endif;


vdbucks 07-01-2013 10:14 PM

Quote:

Originally Posted by MainstreamGuy (Post 19695669)
I'm always surprised by the amount of people on GFY that are PHP experts.

It's like the 2013 average webmaster knows about everything, programming, marketing, design, blogging, tubes, etc etc...

They are true geniuses when you compare them with webmasters from year 2005.

Strange, I didn't see anyone claim to be a php expert. All I saw was someone ask a question and 2 people attempting to answer that question.

I'm always surprised at how many more trolls than non-trolls there are on gfy...

It's like the 2013 average GFY member is nothing but a waste of space, offers nothing but garbage in their posts and serves no other purpose than to disrupt the few of us who are actually trying to get shit done.

BSleazy 07-01-2013 10:36 PM

What you posted above should work but it's not. There's something else I'm missing, I didn't post all the code. I'll screw around with it tomorrow when I'm not tired. Thanks for helping though!


All times are GMT -7. The time now is 11:08 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc