WordPress.org

Ready to get started?Download WordPress

Forums

Adding postmeta info to Popularity contest (3 posts)

  1. aonxe
    Member
    Posted 5 years ago #

    Ok, so I am trying to get a custom field from the postmeta table so that I can display an image next to the link title with the Popularity Contest plugin. I am getting close, I just am doing something wrong, probably something simple.

    Here is the code

    function show_top_ranked($limit, $before, $after) {
    		global $wpdb;
    		$temp = $wpdb;
    
    		$join = apply_filters('posts_join', '');
    		$where = apply_filters('posts_where', '');
    		$groupby = apply_filters('posts_groupby', '');
    		if (!empty($groupby)) {
    			$groupby = ' GROUP BY '.$groupby;
    		}
    		else {
    			$groupby = ' GROUP BY '.$wpdb->posts.'.ID ';
    		}
    
    		$posts = $wpdb->get_results("
    			SELECT *
    			FROM $wpdb->posts
    			LEFT JOIN $wpdb->ak_popularity pop
    			ON $wpdb->posts.ID = pop.post_id
    			$join
    			WHERE post_status = 'publish'
    			AND post_date < NOW()
    			$where
    			$groupby
    			ORDER BY pop.total DESC
    			LIMIT ".intval($limit)
    		);
    
    		if ($posts) {
    			foreach ($posts as $post) {
    			$result = "SELECT * FROM $wpdb->postmeta WHERE post_id = $post->ID AND meta_key = 'Thumbnail'";
    			$postmeta = $wpdb->query( $result );
        			print(
    					$before.'<a href="'.get_permalink($post->ID).'"><img src="http://locahost/aoxne/wp-content/themes/hacknmod/thumb.php?src='.$postmeta->meta_value.'&amp;h=100&amp;w=125&amp;zc=1&amp;q=80"/></a><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a>'.$after
        			);
    			}
    		}
    		else {
    			print($before.'(none)'.$after);
    		}
    		$wpdb = $temp;
    	}

    The problem comes where I am trying to get my query to go off.

    I tried using get_post_meta($post->ID, "Thumbnail", $single = true) before I even wrote the query but it didn't work, anyone have any suggestions?

  2. seville76
    Member
    Posted 5 years ago #

    Hey Aonxe,

    I'm looking for the same, did you find a solution yet?

    Thanks a lot

  3. joekekloosterman
    Member
    Posted 5 years ago #

    Hi,

    I found the solution to your problem.
    Here's the code that works for me:

    if ($posts) {
    			foreach ($posts as $post) {
    			$result = "SELECT * FROM $wpdb->postmeta WHERE post_id = $post->ID AND meta_key = 'postuserpic'";
    			//echo $result;
    			$postmeta = $wpdb->get_results($result);
    			 			print(
        				$before.'<img src="'.get_bloginfo('home')."/wp-content/uploads/thumbs".$postmeta[0]->meta_value.'"/><a href="'.get_permalink($post->ID).'">'
        				.$post->post_title.'</a>'.$after
        			);
    			}
    		}

    I used this on WordPress 2.7 and everything works fine.

Topic Closed

This topic has been closed to new replies.

About this Topic