<?php

$pageId =$_GET['fld_pk_kasjroetId'];

if (!is_numeric($pageId))  die();


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


$meta_key1 = 'ptype';
$meta_key1_value = 'kasjroetlijst';
$meta_key3 = 'old_id';
$meta_key3_value = $pageId;
 $wpdb->show_errors();    
$postids=$wpdb->get_col($wpdb->prepare("
SELECT      key3.post_id 
FROM        $wpdb->postmeta key3
INNER JOIN  $wpdb->postmeta key1 
            on key1.post_id = key3.post_id
            and key1.meta_key = %s
WHERE       key3.meta_key = %s 
            and key3.meta_value = %s 
			and key1.meta_key = %s 
			and key1.meta_value = %s
ORDER BY    key3.meta_value", $meta_key1, $meta_key3,$meta_key3_value,$meta_key1,$meta_key1_value)); 


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' AND post_status = 'publish'");         
 
$url = get_page_link($id, $leavename, $sample) ;

/*
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> 
*/  

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