<?php

/*
* https://www.billerickson.net/code/wp_query-arguments/
*/


function wosmpl_get_geolocalized_contents() {
    $contents = [];
    // WP_Query arguments
    $args = array(
        'post_type'              => array( 'wosmpl_partners' ),
        'order'                  => 'ASC',
        'orderby'                => 'ID',
        'posts_per_page' => '-1',
        'meta_query'             => array(
            
            array(
                'key'     => 'wosmpl_latlon',
                
            )
        ),
    );

    // The Query
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) {
        
        while ( $query->have_posts() ) {
            $query->the_post();
            $p = get_post();
            $metadata = get_post_meta( $p->ID);
            //var_dump($p);
            $c = new StdClass();
            $c->title = $p->post_title;
            $c->type = $p->post_type;
            //These queries should be in cache (no pain after the first time)
            $c->latlon = str_replace(' ','',get_post_meta($p->ID,'wosmpl_latlon', true));
            $c->cat = $metadata['wosmpl_geo_cat'][0];
            $c->presta_type = $metadata['wosmpl_presta_type'][0];
            $c->url = get_permalink($p->ID);
            $c->picture_url = $metadata['wosmpl_partner_pict'][0];
            $c->open_hours = $metadata['wosmpl_partner_open_hours'][0];
            $c->post_addr = $metadata['wosmpl_post_addr'][0];
            $c->specialty = $metadata['wosmpl_partner_specialty'][0];
            $contents[] = $c;
        }
        
        wp_reset_postdata();
    }
  
    return $contents;
}

function wosmpl_get_page_matching_kohinos_id ($kid) {
    //$c = wosmpl_get_geolocalized_contents();
    $pid = NULL;
    $args = array(
	 'post_type'              => array( 'wosmpl_partners' ),
	 'order'                  => 'ASC',
	 'orderby'                => 'ID',
	 'posts_per_page' => '-1',
	 'meta_key' => 'wosmpl_kid',
	 'meta_value' => $kid
	 );

    $query = new WP_Query( $args );
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();
            $p = get_post();
            $pid = $p->ID;
        }
	wp_reset_postdata();
    }

    return $pid;
    //return $c
}

function wosmpl_get_page_matching_kohinos_raison ($raison)
{
    $post = get_page_by_title($raison, 'OBJECT', 'wosmpl_partners');

    return $post->ID ?? null;
}
