Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Updated related posts (by custom field) for post type only WordPress

  • SOLVED

I have a code snippet that was working to show related posts that had a matching custom field.

It was working great, and now i need it to work for a custom post typ.

My attempt is below, but it just crashed the site (even the backend!! eek!).

/***** dSHOP item related ***/


function dshop_article_series() {

global $post;
$currentID = $post->ID;
$series = get_post_meta($post->ID, 'site_store', true);
if($series) :
$args = array(
'numberposts' => 5,
'meta_key' => 'site_store',
'meta_value' => $series,
'orderby'=>rand,
);

$series_posts = get_posts($args);

echo '<div id="edit-related-list" style="background:#ffffff; width:620px; height: 140px; padding-left:15px; padding-top: 10px;"><h3 class="sidebar" style="color: #747284; font-variant: small-caps; font-size: 16px; letter-spacing: 2px; padding-bottom: 5px; padding-top: 10px;">' . __('More from this store') . '</h3><ul>';
foreach($series_posts as $post){
if($post) :
setup_postdata($post);
$image_link = get_first_image();

if ($image_link != "external"){
echo '<li><div class="post_wp_thumb_comm" style="text-align: center;"> <a href="' . get_permalink($post->ID) . '" title="' . str_replace('"', '"', $post->post_title) . '"> <img src="/wp-content/themes/thesis_18/custom/scripts/timthumb.php?src='. $image_link .'&h=100&w=100&zc=1" alt=""></a></div></li>';
} else {
echo '<li> <div class="post_wp_thumb_comm" style="text-align: center;"><a href="' . get_permalink($post->ID) . '" title="' . str_replace('"', '"', $post->post_title) . '"> <img src="'. get_first_image_2() .'" style="max-width:100px; " alt=""></a></div></li>';
}

endif;
}
echo '</ul></div>';
endif;
wp_reset_query();
}

function dshop_label_series() {

global $post;
$currentID = $post->ID;
$series = get_post_meta($post->ID, 'dshop_cat', true);
if($series) :
$args = array(
'numberposts' => 5,
'meta_key' => 'dshop_cat',
'meta_value' => $series,
'orderby'=>rand,
);

$series_posts = get_posts($args);

echo '<div id="edit-related-list" style="background:#ffffff; width:620px; height:140px; padding-left:15px; padding-top: 10px;"><h3 class="sidebar" style="color: #747284; font-variant: small-caps; font-size: 16px; letter-spacing: 2px; padding-bottom: 5px; padding-top: 10px;">' . __('More like this') . '</h3><ul>';

foreach($series_posts as $post){
if($post) :
setup_postdata($post);
$image_link = get_first_image();
if ($image_link != "external"){

echo '<li><div class="post_wp_thumb_comm" style="text-align: center;"> <a href="' . get_permalink($post->ID) . '" title="' . str_replace('"', '"', $post->post_title) . '"> <img src="/wp-content/themes/thesis_18/custom/scripts/timthumb.php?src='. $image_link .'&h=100&w=100&zc=1" alt=""></a></div></li>';
} else {
echo '<li> <div class="post_wp_thumb_comm" style="text-align: center;"><a href="' . get_permalink($post->ID) . '" title="' . str_replace('"', '"', $post->post_title) . '"> <img src="'. get_first_image_2() .'" style="max-width:100px; " alt=""></a></div></li>';
}

endif;
}
echo '</ul></div>';
endif;
wp_reset_query();
}




function related_dshop_edit() {
if (is_single()) {
if ('dshop' == get_post_type()) {

dshop_label_series();

dshop_article_series();

}
}
}

add_action('thesis_hook_after_post_box', 'related_dshop_edit', 99);



working code, for post cat 5121



/***** dSHOP item related ***/


function dshop_article_series() {

global $post;
$currentID = $post->ID;
$series = get_post_meta($post->ID, 'site_store', true);
if($series) :
$args = array(
'numberposts' => 5,
'meta_key' => 'site_store',
'meta_value' => $series,
'orderby'=>rand,
);

$series_posts = get_posts($args);

echo '<div id="edit-related-list" style="background:#ffffff; width:620px; height: 140px; padding-left:15px; padding-top: 10px;"><h3 class="sidebar" style="color: #747284; font-variant: small-caps; font-size: 16px; letter-spacing: 2px; padding-bottom: 5px; padding-top: 10px;">' . __('More from this store') . '</h3><ul>';
foreach($series_posts as $post){
if($post) :
setup_postdata($post);
$image_link = get_first_image();

if ($image_link != "external"){
echo '<li><div class="post_wp_thumb_comm" style="text-align: center;"> <a href="' . get_permalink($post->ID) . '" title="' . str_replace('"', '"', $post->post_title) . '"> <img src="/wp-content/themes/thesis_18/custom/scripts/timthumb.php?src='. $image_link .'&h=100&w=100&zc=1" alt=""></a></div></li>';
} else {
echo '<li> <div class="post_wp_thumb_comm" style="text-align: center;"><a href="' . get_permalink($post->ID) . '" title="' . str_replace('"', '"', $post->post_title) . '"> <img src="'. get_first_image_2() .'" style="max-width:100px; " alt=""></a></div></li>';
}

endif;
}
echo '</ul></div>';
endif;
wp_reset_query();
}

function dshop_label_series() {

global $post;
$currentID = $post->ID;
$series = get_post_meta($post->ID, 'dshop_cat', true);
if($series) :
$args = array(
'numberposts' => 5,
'meta_key' => 'dshop_cat',
'meta_value' => $series,
'orderby'=>rand,
);

$series_posts = get_posts($args);

echo '<div id="edit-related-list" style="background:#ffffff; width:620px; height:140px; padding-left:15px; padding-top: 10px;"><h3 class="sidebar" style="color: #747284; font-variant: small-caps; font-size: 16px; letter-spacing: 2px; padding-bottom: 5px; padding-top: 10px;">' . __('More like this') . '</h3><ul>';

foreach($series_posts as $post){
if($post) :
setup_postdata($post);
$image_link = get_first_image();
if ($image_link != "external"){

echo '<li><div class="post_wp_thumb_comm" style="text-align: center;"> <a href="' . get_permalink($post->ID) . '" title="' . str_replace('"', '"', $post->post_title) . '"> <img src="/wp-content/themes/thesis_18/custom/scripts/timthumb.php?src='. $image_link .'&h=100&w=100&zc=1" alt=""></a></div></li>';
} else {
echo '<li> <div class="post_wp_thumb_comm" style="text-align: center;"><a href="' . get_permalink($post->ID) . '" title="' . str_replace('"', '"', $post->post_title) . '"> <img src="'. get_first_image_2() .'" style="max-width:100px; " alt=""></a></div></li>';
}

endif;
}
echo '</ul></div>';
endif;
wp_reset_query();
}




function related_dshop_edit() {
if (is_single()) {
if(in_category( '5121' )) {

dshop_label_series();

dshop_article_series();

}
}
}

add_action('thesis_hook_after_post_box', 'related_dshop_edit', 99);



*** UPDATE***

I removed the space from this line : if ( 'dshop' == get_post_type()) {
and the site no longer crashes, but the related items don't show up either.

Answers (1)

2011-07-31

Peter Michael answers:

<blockquote>My attempt is below, but it just crashed the site (even the backend!! eek!).</blockquote>

Any error message?


kateM82 comments:

nope, just white screen of death.

i've tried a whole bunch of different ways to call the custom post type and had now luck - it know it's this line that's the issue.

if ( 'dshop' == get_post_type())


Peter Michael comments:

Add to your functions.php at the top

error_reporting(E_ALL);

to see any errors. You can try this updated function as well (untested)

function related_dshop_edit() {
if (is_single()) {
global $post;
$post_type = get_post_type($post->ID)
if ( $post_type == 'dshop' ) {
dshop_label_series();
dshop_article_series();
}
}
}


Peter Michael comments:

Might also add your custom post type to the args array:

$args = array(
'numberposts' => 5,
'post_type' => 'dshop',
'meta_key' => 'dshop_cat',
'meta_value' => $series,
'orderby'=>rand,
);


Peter Michael comments:

My previous function code had a syntax error, here's the updated one:

function related_dshop_edit() {
if( is_single() ) {
global $post;
$post_type = get_post_type($post->ID);
if( $post_type == 'dshop' ) {
dshop_label_series();
dshop_article_series();
}
}
}


kateM82 comments:

Ok, the box is now showing below the posts, but no related items (from the custom post type) are showing in it, see here: http://dropdeadgorgeousdaily.com/dshop/cooper-st-chakra-dress/

Only one item shows, and it is from the original non-custom-post-type posts


Peter Michael comments:

Have you added your custom post type to the args array as mentioned above?


kateM82 comments:

Thanks Peter, that worked (sorry didn't see it before I posted!)


Peter Michael comments:

;-)