WordPress.org

Ready to get started?Download WordPress

Forums

Should be simple, but need some help doing this (3 posts)

  1. Dave P.
    Member
    Posted 5 years ago #

    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.

  2. MichaelH
    Member
    Posted 5 years ago #

  3. @CallMeDPit, @MichaelH - this can be done with Yet Another Related Posts Plugin 3.0's templating feature: http://mitcho.com/blog/projects/yarpp-3-templates/

Topic Closed

This topic has been closed to new replies.

About this Topic