This code works:
$row = array(5,6,89,97,101);
$found = array();
$post = 89;
$count_row = count($row);
for($i = 0; $i < $count_row; $i++){
if($row[$i]==$post){
$found[] = $row[$i-2];
$found[] = $row[$i-1];
$found[] = $row[$i];
$found[] = $row[$i+1];
$found[] = $row[$i+2];
var_dump($found);
}
}
And this does not, probably doing something wrong in the mysql_fetch_array;
$found = array();
$q = "SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id='$term_tax_id'";
$rs = mysql_query($q) or die(mysql_error());
$row = mysql_fetch_array($rs, MYSQL_NUM);
$count_row = count($row);
for($i = 0; $i < $count_row; $i++){
print_r($row);
if($row[$i]==$post){
$found[] = $row[$i-2];
$found[] = $row[$i-1];
$found[] = $row[$i];
$found[] = $row[$i+1];
$found[] = $row[$i+2];
var_dump($found);
}
}
Nothing is shown if post is more than 1.
Anyone know a way to solve this problem?
The select statement should be rewritten so as to return only the values you’re looking for. One way to do this is with a
UNIONof two selects, one returning lesser object IDs and one returning greater. You should also make use of WordPress’swpdbclass. For one thing, a site admin may change the table prefix from “wp_” to something else;$wpdb->term_relationshipswill give the correct name for the table.This also has the advantage of working when there are less than two terms relationships before or after the center (the object with id
$post).