<?php
/**
 * Template Name: Jobs listing
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package WordPress
 * @subpackage Biuro
 * @since 1.0
 * @version 1.0
 */

get_header(); ?>

<?php

    while ( have_posts() ) :

        the_post();
        the_content();

    endwhile;
?>

<?php

    $ID =  get_the_ID();
    $keys =  get_post_custom_keys();

    $taxonomies = array();

    foreach ( $keys as $key => $value ) {
        $val = trim($value);
        $taxonomy = substr($val, 20);

        if ( '_' == $val{0} || substr($val, 0, 20) != 'built_in_taxonomies_' || !$taxonomy):
            continue;
        endif;

        $res = get_post_custom_values( $val, $ID );

        if ( !$res ) {
            continue;
        }

        $term = $res[0];

        if ( !$term ) {
            continue;
        }

        $taxonomies[] = array(
                'taxonomy' => $taxonomy,
                'field' => 'slug',
                'terms' => $term
            );
    }

    $jobs = get_posts( array(
        'posts_per_page' => -1,
        'post_type' => 'job',
        'tax_query' => $taxonomies
    ) );

    // debug($jobs);

    foreach ($jobs as $job) :
        echo '<h3><a href="' . get_post_permalink( $job->ID ) . '" target="_blank">' . $job->post_title . '</a></h3>';
    endforeach;

?>

<?php get_footer();
