• Resolved Franz Buttssortht

    (@ericpitcock)


    First post – please be kind ??

    I have a custom field – ‘thumb’ – in all my posts. Called using the typical:

    <?php get_post_meta($post->ID, 'thumb', true); ?>

    Which just spits out the image path.

    I want to spit out ‘thumb’ for EVERY post in the WORK category, outside the loop, on the WORK category page.

    I experimented with wp_query, but nothing really worked for me. I’m a PHP rookie.

    I appreciate any help. Thank you!

Viewing 5 replies - 1 through 5 (of 5 total)
  • You’ll need to query the database.

    Try this:

    $thumbs = $wpdb->get_col("
    	SELECT
    		p.meta_value
    	FROM $wpdb->postmeta p
    	INNER JOIN $wpdb->term_relationships a
    	ON p.post_id = a.object_id
    	JOIN $wpdb->term_taxonomy b
    	USING (term_taxonomy_id)
    	JOIN $wpdb->terms c
    	USING (term_id)
    	WHERE p.meta_key = 'thumb'
    	AND c.name = 'WORK'
    ");
    
    foreach ($thumbs as $thumb)
    	echo $thumb->meta_value;

    Thread Starter Franz Buttssortht

    (@ericpitcock)

    That actually worked perfectly, after I removed:

    AND c.name = 'WORK'

    Thanks!

    Thread Starter Franz Buttssortht

    (@ericpitcock)

    And as a good support forum user, here’s how this was implemented. I wanted to spit out the meta_value (image path) for all my portfolio images, so I could create a one-click slideshow with Colorbox. You will notice I added “ORDER BY meta_value ASC” to get them in order.

    <?php
    			// this yanks the thumb from every post
    			$thumbs = $wpdb->get_col("
    			SELECT p.meta_value
    			FROM $wpdb->postmeta p
    			INNER JOIN $wpdb->term_relationships a
    			ON p.post_id = a.object_id
    			JOIN $wpdb->term_taxonomy b
    			USING (term_taxonomy_id)
    			JOIN $wpdb->terms c
    			USING (term_id)
    			WHERE p.meta_key = 'thumb'
    			ORDER BY meta_value ASC
    			");?>
    
    			<?php foreach ($thumbs as $thumb){
    				echo
    				'<a href="'.get_bloginfo('template_directory').'/'.$thumb.'" rel="slideshow"></a>
    				';
    			} ?>

    It’s not absolutely necessary, but if I could find a way to exclude certain ones whenever I wanted, that would make this perfect.

    Thread Starter Franz Buttssortht

    (@ericpitcock)

    Exclude by meta_value, that is.

    c.name = 'WORK' is the category name and I just copy it exactly from your first post. I probably should have changed it to 'Work' to avoid the confusion.

    Also, just want to correct a mistake in my first post in case other’s are using it. $wpdb->get_col returns an array not an object, therefore

    foreach ($thumbs as $thumb)
    	echo $thumb->meta_value;

    should just be

    foreach ($thumbs as $thumb)
    	echo $thumb;

    BTW, can you put this as resolved? Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Calling a particular custom field from ALL posts’ is closed to new replies.