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

restrict related posts script to single category WordPress

  • SOLVED

Hi,

I had this questions brilliantly solved for me a little while back:
http://wpquestions.com/question/show/id/2150

The final working code is : 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; padding-left:15px; padding-top: 10px;><h4 class="series-title" style="border-top: 1px solid #EFEFEF; color: #111111; padding-top: 10px;">' . __('More like this') . '</h4><ul>';

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

if ($image_link != "external"){
echo '<li><img src="/wp-content/themes/thesis_18/custom/scripts/timthumb.php?src='. $image_link .'&h=80&w=80&zc=1" alt=""><br/><a href="' . get_permalink($post->ID) . '" title="' . str_replace('"', '"', $post->post_title) . '">' . str_replace('"', '"', $post->post_title) . '</a></li>';
} else {
echo '<li><img src="'. get_first_image_2() .'" height="80" alt=""><br/><a href="' . get_permalink($post->ID) . '" title="' . str_replace('"', '"', $post->post_title) . '">' . str_replace('"', '"', $post->post_title) . '</a></li>';
}

endif;
}
echo '</ul><img alt="" src="http://dropdeadgorgeousdaily.com/wp-content/uploads/whitediv.gif"></div>';
endif;
wp_reset_query();
}



I would like to now make it only grab related posts from a certain category as well.

Thanks

Answers (3)

2011-06-11

Nilesh shiragave answers:

Just add 'category'=>2 in $args array. replace 2 with your category id.

final code


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,
'category'=>2

);



$series_posts = get_posts($args);



echo '<div id="edit-related-list" style="background:#ffffff; width:620px; padding-left:15px; padding-top: 10px;><h4 class="series-title" style="border-top: 1px solid #EFEFEF; color: #111111; padding-top: 10px;">' . __('More like this') . '</h4><ul>';



foreach($series_posts as $post){

if($post) :

setup_postdata($post);

$image_link = get_first_image();



if ($image_link != "external"){

echo '<li><img src="/wp-content/themes/thesis_18/custom/scripts/timthumb.php?src='. $image_link .'&h=80&w=80&zc=1" alt=""><br/><a href="' . get_permalink($post->ID) . '" title="' . str_replace('"', '"', $post->post_title) . '">' . str_replace('"', '"', $post->post_title) . '</a></li>';

} else {

echo '<li><img src="'. get_first_image_2() .'" height="80" alt=""><br/><a href="' . get_permalink($post->ID) . '" title="' . str_replace('"', '"', $post->post_title) . '">' . str_replace('"', '"', $post->post_title) . '</a></li>';

}



endif;

}

echo '</ul><img alt="" src="http://dropdeadgorgeousdaily.com/wp-content/uploads/whitediv.gif"></div>';

endif;

wp_reset_query();

}



Replace 'category'=>2 by your category id.


Nilesh shiragave comments:

@Julian Lannigan we are here to answer questions not to debate who's question is good to read or not..

I tested my code it works fine..


kateM82 comments:

I think Julian meant my code was a bit messy in the first place, don't worry.

This worked great, I have combined the two.

Thanks

2011-06-11

Julian Lannigan answers:

Hi Kate,

Nilesh answered faster than me, but I am such a stickler for clean code that I formatted your code better. It was quite a mess to read, no offense. Attached is the code that you can copy and paste. I added a variable "$restrictToCategoryID" that is the id of the category to which you wish to restrict the query to.

function dshop_label_series() {
global $post;
$currentID = $post->ID;
$restrictToCategoryID = 0; //Change this to the desired category
$series = get_post_meta($post->ID, 'dshop_cat', true);
if($series) {
$args = array(
'numberposts' => 5,
'meta_key' => 'dshop_cat',
'meta_value' => $series,
'orderby'=>rand,
'category' => $restrictToCategoryID
);

$series_posts = get_posts($args);
echo '<div id="edit-related-list" style="background:#ffffff; width:620px; padding-left:15px; padding-top: 10px;><h4 class="series-title" style="border-top: 1px solid #EFEFEF; color: #111111; padding-top: 10px;">' . __('More like this') . '</h4><ul>';

foreach($series_posts as $post){
if($post) {
setup_postdata($post);
$image_link = get_first_image();

if ($image_link != "external"){
echo '<li><img src="/wp-content/themes/thesis_18/custom/scripts/timthumb.php?src='. $image_link .'&h=80&w=80&zc=1" alt=""><br/><a href="' . get_permalink($post->ID) . '" title="' . str_replace('"', '"', $post->post_title) . '">' . str_replace('"', '"', $post->post_title) . '</a></li>';
} else {
echo '<li><img src="'. get_first_image_2() .'" height="80" alt=""><br/><a href="' . get_permalink($post->ID) . '" title="' . str_replace('"', '"', $post->post_title) . '">' . str_replace('"', '"', $post->post_title) . '</a></li>';
}
}
}
echo '</ul><img alt="" src="http://dropdeadgorgeousdaily.com/wp-content/uploads/whitediv.gif"></div>';
}
wp_reset_query();
}


kateM82 comments:

No offence taken... I am really just stumbling my way through all this :)


Julian Lannigan comments:

@Kate
Programming can sometimes be a trial by fire type deal, so you learn as you do most of the time. ;)

@Nilesh shiragave
<blockquote>Nilesh answered faster than me, but I am such a stickler for clean code that I formatted your code better.</blockquote>
When I said "your" I meant Kate's code, because my point-of-view is aimed at the asker when I answer a question. It is not a dis on anyone on this site rather my OCD for clean code.
When I said "Nilesh answered faster than me" I was inferring you answered faster and with the same answer that I gave. You basically copied, edited, and pasted the same code with the correction, <em>there is nothing wrong with this</em>. I simply provided Kate with cleaner code that took me all of 2 minutes to give.
<blockquote>we are here to answer questions not to debate who's question is good to read or not.</blockquote>
Who's debating? You have your account and I have mine, when asker's ask a question multiple people will most likely try and give their best answer for it. That was my best answer, and my best answer includes a cleaner version of Kate's code, not yours.
And would you not agree, it's a lot easier to read code if it's formatted correctly? I was just helping out.

<strong>Please respect everyone's ability to answer these questions.</strong>

2011-06-11

Christianto answers:

I think we use 'cat' not 'category' as seen on [[LINK href="http://codex.wordpress.org/Function_Reference/WP_Query#Category_Parameters"]]category query parameter[[/LINK]]


Christianto comments:

if 'category' works..
that's great..