<?php
    /*
        Template Name: Random node
    */


    $last = get_posts(
        array(
            'post_status' => 'publish',
            'post_type' => array('anew','profile_article','yellow'),
            'posts_per_page' => 5,
            'orderby' => 'date',
            'order' => 'DESC'
        )
    );

    $posts = json_decode(get_option('ppp_options'));

    $count = (int)get_option('randomnode_count');
    $count = $count+1;
    update_option('randomnode_count', $count);

    //var_dump();
    //die;
    $popular = get_posts(
        array(
            'post__in' => array_map(function($obj){return $obj->id;},$posts),
            'ignore_sticky_posts' => 1,
            'post_type' => array('anew', 'yellow', 'profile_article'),
            'posts_per_page' => 20
        )
    );

    foreach ($popular as $key => $item){
        if(get_post_meta($item->id,'popular_hide', true) == 1){ unset($popular[$key]); }
    }


    $posts = array_merge($last, array_slice($popular,0,5));

    //var_dump($posts);

    wp_redirect(get_permalink($posts[rand(0,count($posts))]), 301);
    exit;
?>