• I’m using a Related Posts plugin, and instead of the title of a post, I’d like to be able to get a custom field from the post (in this instance “TitleImage”, which is a thumb for the post). The current code to get the title data is as follows:

    // Primary SQL query
    
        $sql = "SELECT ID, post_title, post_content,"
             . "MATCH (post_name, post_content) "
             . "AGAINST ('$terms') AS score "
             . "FROM $wpdb->posts WHERE "
             . "MATCH (post_name, post_content) "
             . "AGAINST ('$terms') "
    		 . "AND post_date <= '$now' "
             . "AND (post_status IN ( 'publish',  'static' ) && ID != '$post->ID') ";
        if ($show_pass_post=='false') { $sql .= "AND post_password ='' "; }
        $sql .= "ORDER BY score DESC LIMIT $limit";
        $results = $wpdb->get_results($sql);
        $output = '';
        if ($results) {
            if($return_bool) {
    			return true;
    		}
    		else {
    			foreach ($results as $result) {
    				$title = stripslashes(apply_filters('the_title', $result->post_title));
    				$permalink = get_permalink($result->ID);
    				$post_content = strip_tags($result->post_content);
    				$post_content = stripslashes($post_content);
    				$output .= $before_title .'<a href="'. $permalink .'" rel="bookmark" title="Permanent Link: ' . $title . '">' . $title . '</a>' . $after_title;
    				if ($show_excerpt=='true') {
    					$ze = substr($post_content, 0, $len);
    					$ze = substr($ze, 0, strrpos($ze,''));
    					$ze = $ze . '...';
    					$output .= $before_post . $ze . $after_post;
    //					$words=split(" ",$post_content);
    //					$post_strip = join(" ", array_slice($words,0,$len));
    //					$output .= $before_post . $post_strip . $after_post;
    				}
    			}
    			echo $output;
    		}
    	} else {
            if($return_bool) {
    			return false;
    		} else {
    			echo $before_title.'No related posts'.$after_title;
    		}
        }
    }

    I tried playing around with some things, but couldn’t get it. So how can I turn this into being able to get the custom field from the post “TitleImage” if it exists…if not, just show the title.

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Should be simple, but need some help doing this’ is closed to new replies.