PHP question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BSleazy
    Confirmed User
    • Aug 2002
    • 6721

    #1

    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 );
    }
    icq 156131086
  • vdbucks
    Monger Cash
    • Jul 2010
    • 2773

    #2
    Originally posted by BCyber
    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
    Last edited by vdbucks; 07-01-2013, 08:01 PM.

    Comment

    • Colmike9
      (>^_^)b
      • Dec 2011
      • 7230

      #3
      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.
      Last edited by Colmike9; 07-01-2013, 07:59 PM.
      Join the BEST cam affiliate program on the internet!
      I've referred over $1.7mil in spending this past year, you should join in.
      I make a lot more money in the medical field in a lab now, fuck you guys. Don't ask me to come back, but do join Chaturbate in my sig, it still makes bank without me touching shit for years..

      Comment

      • BSleazy
        Confirmed User
        • Aug 2002
        • 6721

        #4
        Originally posted by Colmike7
        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.
        icq 156131086

        Comment

        • vdbucks
          Monger Cash
          • Jul 2010
          • 2773

          #5
          Originally posted by BCyber
          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.

          Comment

          • MainstreamGuy
            So Fucking Banned
            • Oct 2011
            • 477

            #6
            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.

            Comment

            • BSleazy
              Confirmed User
              • Aug 2002
              • 6721

              #7
              Originally posted by vdbucks
              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.
              icq 156131086

              Comment

              • vdbucks
                Monger Cash
                • Jul 2010
                • 2773

                #8
                Originally posted by BCyber
                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;

                Comment

                • vdbucks
                  Monger Cash
                  • Jul 2010
                  • 2773

                  #9
                  Originally posted by MainstreamGuy
                  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.
                  Last edited by vdbucks; 07-01-2013, 09:26 PM.

                  Comment

                  • BSleazy
                    Confirmed User
                    • Aug 2002
                    • 6721

                    #10
                    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!
                    icq 156131086

                    Comment

                    Working...