Advertisement
dimkiriakos

Wordpress Custom Routes

Apr 29th, 2022
1,406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.74 KB | None | 0 0
  1. <?php
  2.  
  3. add_action('rest_api_init', 'universityRegisterSearch');
  4.  
  5. function universityRegisterSearch(){
  6.     register_rest_route('university/v1', 'search', [
  7.         'methods' => WP_REST_Server::READABLE, // Wordpress alias for the GET method (we could write also GET)
  8.         'callback' => 'universitySearchResults', // callback name of a function that we will use
  9.     ]);
  10. }
  11.    
  12. // The callback function
  13. function universitySearchResults($data) {
  14.     $mainQuery = new WP_Query(array(
  15.       'posts_per_page' => -1,
  16.       'post_type' => array('post', 'page', 'professor', 'program', 'campus', 'event'),
  17.       's' => sanitize_text_field($data['term'])
  18.     ));
  19.  
  20.     $results = array(
  21.       'generalInfo' => array(),
  22.       'professors' => array(),
  23.       'programs' => array(),
  24.       'events' => array(),
  25.       'campuses' => array()
  26.     );
  27.  
  28.     while($mainQuery->have_posts()) {
  29.       $mainQuery->the_post();
  30.  
  31.       if (get_post_type() == 'post' OR get_post_type() == 'page') {
  32.         array_push($results['generalInfo'], array(
  33.           'title' => get_the_title(),
  34.           'permalink' => get_the_permalink(),
  35.           'postType' => get_post_type(),
  36.           'authorName' => get_the_author()
  37.         ));
  38.       }
  39.  
  40.       if (get_post_type() == 'professor') {
  41.         array_push($results['professors'], array(
  42.           'title' => get_the_title(),
  43.           'permalink' => get_the_permalink(),
  44.           'image' => get_the_post_thumbnail_url(0, 'professorLandscape'), // 0 means the current post
  45.         ));
  46.       }
  47.  
  48.       if (get_post_type() == 'program') {
  49.         $relatedCampuses = get_field('related_campus');
  50.  
  51.         if ($relatedCampuses) {
  52.           foreach ($relatedCampuses as $campus) {
  53.             array_push($results['campuses'], [
  54.               'title' => get_the_title($campus),
  55.               'permalink' => get_the_permalink($campus),
  56.             ]);
  57.           }
  58.         }
  59.  
  60.         array_push($results['programs'], array(
  61.           'title' => get_the_title(),
  62.           'permalink' => get_the_permalink(),
  63.           'id' => get_the_ID(),
  64.         ));
  65.       }
  66.  
  67.       if (get_post_type() == 'campus') {
  68.         array_push($results['campuses'], array(
  69.           'title' => get_the_title(),
  70.           'permalink' => get_the_permalink()
  71.         ));
  72.       }
  73.      
  74.       if (get_post_type() == 'event') {
  75.         $eventDate = new DateTime(get_field('event_date'));
  76.         $month = $eventDate->format('M');
  77.         $day = $eventDate->format('d');
  78.         array_push($results['events'], array(
  79.           'title' => get_the_title(),
  80.           'permalink' => get_the_permalink(),
  81.           'month' => $month,
  82.           'day' => $day,
  83.           'excerpt' => has_excerpt() ? get_the_excerpt() : wp_trim_words(get_the_content(), 18),
  84.          ));
  85.       }
  86.      
  87.     }
  88.  
  89.     if ($results['programs']){
  90.       $programsMetaQuery = [
  91.         'relation' =>'OR',
  92.       ];
  93.    
  94.       foreach($results['programs'] as $item){
  95.         $programsMetaQuery[]= [
  96.           'key' => 'related_programs',
  97.           'compare'=> 'LIKE',
  98.           'value' => '"' . $item['id']. '"',
  99.         ];
  100.       }
  101.       $progamRelationshipQuery = new WP_Query([
  102.         'posts_per_page' => -1,
  103.         'post_type' => [
  104.           'professor',
  105.           'event',
  106.         ],
  107.         'meta_query' => $programsMetaQuery,
  108.       ]);
  109.  
  110.       while($progamRelationshipQuery->have_posts()){
  111.         $progamRelationshipQuery->the_post();
  112.  
  113.  
  114.         if (get_post_type() == 'event') {
  115.           $eventDate = new DateTime(get_field('event_date'));
  116.           $month = $eventDate->format('M');
  117.           $day = $eventDate->format('d');
  118.           array_push($results['events'], array(
  119.             'title' => get_the_title(),
  120.             'permalink' => get_the_permalink(),
  121.             'month' => $month,
  122.             'day' => $day,
  123.             'excerpt' => has_excerpt() ? get_the_excerpt() : wp_trim_words(get_the_content(), 18),
  124.            ));
  125.         }
  126.  
  127.         if (get_post_type() == 'professor') {
  128.           array_push($results['professors'], array(
  129.             'title' => get_the_title(),
  130.             'permalink' => get_the_permalink(),
  131.             'image' => get_the_post_thumbnail_url(0, 'professorLandscape'), // 0 means the current post
  132.           ));
  133.         }
  134.        
  135.        
  136.  
  137.  
  138.  
  139.       }
  140.  
  141.       // array_values removes the numeric keys from associative array that creates the array_unique
  142.       // array_unique removes the duplicate elements from the array and the SORT_REGULAR creates an associative array
  143.       $results['professors'] = array_values(array_unique($results['professors'], SORT_REGULAR));
  144.       $results['events'] = array_values(array_unique($results['events'], SORT_REGULAR));
  145.     }
  146.  
  147.  
  148.     return $results;
  149.  
  150.   }
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement