<?php

	$pageId =$_GET['fld_pk_algListId'];
	if (!is_numeric($pageId))  die('not valid');


	include_once('wp-config.php');
	include_once('wp-load.php');
	include_once('wp-includes/wp-db.php');
	  error_reporting(E_ALL); 

    $meta_key3 = 'old_id';
	$meta_key3_value = $pageId;
	$postids=$wpdb->get_col($wpdb->prepare("
	SELECT      key3.post_id
	FROM        $wpdb->postmeta key3
	WHERE       key3.meta_key = %s 
	            and key3.meta_value = %s
	ORDER BY    key3.meta_value", $meta_key3, $meta_key3_value)); 
 $wpdb->print_error();
	print_r( $postids);
	if ($postids) {
	  echo 'List of ' . $meta_key3_value . '(s), sorted by ' . $meta_key3;
	  foreach ($postids as $id) { 

		$post=get_post(intval($id));
	    setup_postdata($post);
		$post_type=$wpdb->query("SELECT post_type FROM $wpdb->posts WHERE ID = ".$id."  AND post_type = 'post'");         
		
		$url = get_page_link($id, $leavename, $sample) ;

			if ($post_type == 0) {
		 
				header( "Location: /pagenotfound/" );
		
			}else{
				header( "HTTP/1.1 301 Moved Permanently" ); 
				header( "Location: ".$url );

			}
	  } 
	}else{ header( "Location: /pagenotfound/" );}

	?>