WordPress search and Filter API for IOS and Android

I am working on a project to apply filters on wordpress post…I have created JSON Output and Now I need to apply 5 filters that are as follows..
1. All
2. Today
3. WeekEnd
4. Next Seven Days
5. Date wise

Flow of the whole process…GEt Posts data in JSON–> Fetch This Data –> Create Filters—> User can Then Search as well as filter Data Further with normal wordpress functions….But I am not able to work out for fetching out posts by applying the Next Seven days filter and weekEnd..as the issue seems to be at my way of fetching the posts…What I am doing for now is

Code for Next Seven Days or last seven days…

<?php
require($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
//require_once("config.php" );
require_once("incformfunctions.php");


if($_SERVER['REQUEST_METHOD'] == "GET"){
    $numberposts = isset($_REQUEST['numberposts']) ? $_REQUEST['numberposts'] : "-1";   
    $posttype = isset($_REQUEST['posttype']) ? $_REQUEST['posttype'] : "";  
    $securitycode = $_REQUEST['securitycode'];
    $mode = $_REQUEST['mode']? $_REQUEST['mode'] : "ALL";
    $taxonomy='art_categories';


if($securitycode == $secret)
{
$category_data=array(); 
$now = date('Y-m-d');
$start_date = strtotime($now);

$end_date = strtotime("+7 day", $start_date);
//echo date('Y-m-d', $start_date) . '  + 7 days =  ' . date('Y-m-d', $end_date);  die;

    if($mode=='recommended') //to get recommended post of posttype category
    {       

    /*$args = array(*/
    /*'numberposts' => $numberposts,
    'post_type' => 'events',
    'meta_key'  => 'event_start_date',
    'orderby'   => 'meta_value_num',
    'order'     => 'DESC',
     'date_query' => array(

            'after' =>$now,

    ),*/
    /*'posts_per_page'=> -1,'orderby'=>'meta_value','meta_key'=>'event_start_date','order'=>'ASC','post_type'=>'events',       'meta_query'=> array( 
       array('key'=>'event_end_date','value'=> date("Y-m-d H:i"),'compare'=>'>=','type'=>'DATE')


       ),*/
        /*array(  'post_type'   => 'events',  'meta_query' => array(  'relation' => 'OR',  array( 'key' => 'event_end_date', 'value' => date('Ymd'), 'compare' => '>=', 'type' => 'date' ), array( 'key' => 'event_start_date', 'value' => date('Ymd'), 'compare' => '>=', 'type' => 'date' ) ),  'orderby'    => 'meta_value_num',  'order' => 'ASC',  'nopaging' => true ),*/

          /*'numberposts' => -1, 'post_type' => 'film', 'meta_key' => 'showings_%_start_datetime', 
          'meta_value' => array( time(), strtotime( '+7 days' ) ), 'meta_type' => 'NUMERIC', 'meta_compare' => 'BETWEEN', 'orderby' => 'meta_value_num', 'order' => 'ASC', ),*/



/*   meta_query'    => array(
     relation'      => 'AND',
        array(
          key'     => 'event_editors_choice',
         value'     => true,
         relation'      => 'AND',
        ),
        array(
        key'        => 'event_recommended',
        value'      => true
        ),

    )
);*/ 
        $args= array(
        'numberposts' => 100,
        'category' => 0,
         'orderby' => 'date',
        'order' => 'DESC', 'include' => array(),
        'exclude' => array(), 'meta_key' => '',
        'meta_value' =>'', 'post_type' => 'events',
        'suppress_filters' => true,


        'date_query' => array(
        array(



            'key'       => 'event_start_date',
            'after' => '1 week ago'
        ),
        ),


        'meta_query'    => array(
        'relation'      => 'AND',
        array(
          'key'     => 'event_editors_choice',
         'value'        => true
        ),
        'relation'      => 'AND',
        array(
        'key'       => 'event_recommended',
        'value'     => true
        ),),

   );



    }
            $myposts = get_posts( $args);

            // var_dump($myposts);die;
                if($myposts) {



                foreach ( $myposts as $post ) : setup_postdata( $post ); 

                    $image= get_the_post_thumbnail_url();
                    $featured_image =wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ));

                    $start_date= get_post_meta($post->ID,'event_start_date',true);

                    $start_date=date('F j, Y', strtotime($start_date));

                    //$start_date1=date(+, strtotime($start_date));
                    //$start_date1 = strtotime ( '+6 days' , strtotime ( $start_date ) ) ;

                    $end_date = get_field('event_end_date', $post->ID, false);
                    $end_date=date('F j, Y', strtotime($end_date));
                    $event_type = get_field('event_type', $post->ID, true);
                    $event_venue = get_field('event_venue', $post->ID, true);
                    $event_venue_address = get_field('event_venue_address', $post->ID, true);
                    $latitude = get_field('event_venue_latitute', $post->ID, true);
                    $longitude = get_field('event_venue_longitude', $post->ID, true);
                    $description_long = get_field('event_description_long', $post->ID, true);
                    $description_short = get_field('event_description_short', $post->ID,
                    true);
                    $gallery_address = get_field('gallery_address', $post->ID,
                    true);
                    if($gallery_address==false)
                    {
                        $gallery_address="";
                    }
                    $event_gallery = get_post_meta($post->ID,'gallery_address', true);
                    $venue_address= get_field('venue_address',$event_gallery,true);
                    $venue_postcode= get_field('venue_postcode',$event_gallery,true);
                    $venue_city= get_field('venue_city',$event_gallery,true);
                    $venue_location= get_field('venue_location',$event_gallery,true);
                    if($venue_location==false)
                    {
                        $venue_location="";
                    }
                    $venue=get_post_custom($post->ID);
                    $category=get_the_category($post->ID); //category
                    $excerpt=get_the_excerpt( $post );
                    $posttypes=get_post_type( $post );

                                  //$tags = get_tags($post->ID);

                                  //$terms = get_categories($post);

                                  //$terms = wp_get_post_tags( $post->ID,'art_tags');

                                  //$category=get_the_terms( $post->ID, $taxonomy );//to get taxonomy
        $category_data[] = array('id' => get_the_ID (),'title' => get_the_title(),'excerpt' =>$excerpt,'featured_image' =>$featured_image,'image' =>$image,'event_type' =>$event_type,'event_venue' =>$event_venue,'event_venue_address' =>$event_venue_address,'event_latitude' =>$latitude,'event_longitude' =>$longitude,'start_date' =>$start_date,'end_date' =>$end_date,'posttypes' =>$posttypes,'tags'=>$tags,'description_long'=>$description_long,'description_short'=>$description_short,'venue'=>$event_gallery,'gallery_address'=>$gallery_address,'venue_address'=>$venue_address,'venue_postcode'=>$venue_postcode,'venue_city'=>$venue_city,'venue_location'=>$venue_location);      

                    wp_reset_postdata();

                     endforeach;   

//echo '<pre>';  var_dump($category_data);
                    $data = $category_data;

                    $errcode=100;

                    $errstr='success';

                        }

             else       {

                     $errcode=-1;

                    $errstr='Post not found please check again..';

                     }
}
//for securitycheck
if ($securitycode !=$secret or $securitycode=='')
{
    $errstr="unauthorise access";
}//end
} 

else{

    $errcode=-2;
    $errstr='Request method not accepted';
}
@mysql_close($conn);
/* Output header */
    @header('Content-type: application/json');
echo json_encode(array('errcode'=>$errcode,'errstr'=>$errstr,'data'=>$data));
die();

Code for Weekend Filter

<?php
require($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
//require_once("config.php" );
require_once("incformfunctions.php");

if($_SERVER['REQUEST_METHOD'] == "GET"){
    $numberposts = isset($_REQUEST['numberposts']) ? $_REQUEST['numberposts'] : "500";  
    $posttype = isset($_REQUEST['posttype']) ? $_REQUEST['posttype'] : "";  
    $securitycode = $_REQUEST['securitycode'];
    $mode = $_REQUEST['mode']? $_REQUEST['mode'] : "ALL";
    $taxonomy='art_categories';


if($securitycode == $secret)
{
$category_data=array(); 

    //if($mode=='all')//to get all post of posttype category
    //{

    //$args = array( 'posts_per_page' => 108, 'category' =>$taxonomy,'post_type' =>$posttype,'orderby' => 'modified_gmt','offset' => '10' );
    //}

    if($mode=='recommended')//to get recommended post of posttype category
    {
    $args = array(
    'numberposts'   => $numberposts,
    'post_type'     => $posttype,

    'meta_query'    => array(
    'relation'      => 'AND',

        array(
        'key' => 'event_start_date',
        'value' => date("Y-m-d", strtotime("next Saturday")),
        'compare' => '<',
        'type' => 'date'
        ),
        array(
        'key' => 'event_start_date',
        'value' => date("Y-m-d"),
        'compare' => '>',
        'type' => 'date'
        ),

        'relation'      => 'AND',
        array(
          'key'     => 'event_editors_choice',
         'value'        => true
        ),
        'relation'      => 'AND',
        array(
        'key'       => 'event_recommended',
        'value'     => true
        ),
    )
   );
    }

            $myposts = get_posts( $args);
            // var_dump($myposts);die;
                if($myposts) {

                foreach ( $myposts as $post ) : setup_postdata( $post ); 
                    $image= get_the_post_thumbnail_url();
                    $featured_image =wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ));

                    $start_date= get_post_meta($post->ID,'event_start_date',true);

                    $start_date=date('F j, Y', strtotime($start_date));

                    //$start_date1=date(+, strtotime($start_date));
                    //$start_date1 = strtotime ( '+6 days' , strtotime ( $start_date ) ) ;

                    $end_date = get_field('event_end_date', $post->ID, false);
                    $end_date=date('F j, Y', strtotime($end_date));
                    $event_type = get_field('event_type', $post->ID, true);
                    $event_venue = get_field('event_venue', $post->ID, true);
                    $event_venue_address = get_field('event_venue_address', $post->ID, true);
                    $latitude = get_field('event_venue_latitute', $post->ID, true);
                    $longitude = get_field('event_venue_longitude', $post->ID, true);
                    $description_long = get_field('event_description_long', $post->ID, true);
                    $description_short = get_field('event_description_short', $post->ID,
                    true);
                    $gallery_address = get_field('gallery_address', $post->ID,
                    true);
                    if($gallery_address==false)
                    {
                        $gallery_address="";
                    }
                    $event_gallery = get_post_meta($post->ID,'gallery_address', true);
                    $venue_address= get_field('venue_address',$event_gallery,true);
                    $venue_postcode= get_field('venue_postcode',$event_gallery,true);
                    $venue_city= get_field('venue_city',$event_gallery,true);
                    $venue_location= get_field('venue_location',$event_gallery,true);
                    if($venue_location==false)
                    {
                        $venue_location="";
                    }
                    $venue=get_post_custom($post->ID);
                    $category=get_the_category($post->ID); //category
                    $excerpt=get_the_excerpt( $post );
                    $posttypes=get_post_type( $post );

                                  //$tags = get_tags($post->ID);

                                  //$terms = get_categories($post);

                                  //$terms = wp_get_post_tags( $post->ID,'art_tags');

                                  //$category=get_the_terms( $post->ID, $taxonomy );//to get taxonomy
        $category_data[] = array('id' => get_the_ID (),'title' => get_the_title(),'excerpt' =>$excerpt,'featured_image' =>$featured_image,'image' =>$image,'event_type' =>$event_type,'event_venue' =>$event_venue,'event_venue_address' =>$event_venue_address,'event_latitude' =>$latitude,'event_longitude' =>$longitude,'start_date' =>$start_date,'end_date' =>$end_date,'posttypes' =>$posttypes,'tags'=>$tags,'description_long'=>$description_long,'description_short'=>$description_short,'venue'=>$event_gallery,'gallery_address'=>$gallery_address,'venue_address'=>$venue_address,'venue_postcode'=>$venue_postcode,'venue_city'=>$venue_city,'venue_location'=>$venue_location);      

                    wp_reset_postdata();

                     endforeach;   

//echo '<pre>';  var_dump($category_data);
                    $data = $category_data;

                    $errcode=100;

                    $errstr='success';

                        }

             else       {

                     $errcode=-1;

                    $errstr='Post not found please check again..';

                     }
}
//for securitycheck
if ($securitycode !=$secret or $securitycode=='')
{
    $errstr="unauthorise access";
}//end
} 

else{

    $errcode=-2;
    $errstr='Request method not accepted';
}
@mysql_close($conn);
/* Output header */
    @header('Content-type: application/json');
echo json_encode(array('errcode'=>$errcode,'errstr'=>$errstr,'data'=>$data));
die();

Any Help on what I am doing wrong??

Read more here: WordPress search and Filter API for IOS and Android

Leave a Reply

Your email address will not be published. Required fields are marked *