query – Page with custom template make get request to a custom route – Pseudo code

Question

I am assuming you are trying to retrieve data from that route and display that to the end user?

Instead of using the rest API to retrieve that information, you could put the callback function directly into your template file and then use the php $_GET variable (php docs) to pass the search term into your function.
I think it would look like something like this:

 function output_data_from_search_term(){
  $url_param = $_GET['myterm'];
  if(empty($url_param)){
    echo "Search term missing, please try again.";
  }
  //Do whatever query you are doing in your custom rest api endpoint callback. 
  //Here is an example of getting the custom post types with that term and displaying the titles.
  $search_query = urldecode($url_param);
  $query_results = get_posts( array("post_type" => "custom-post-type", "s" => $search_query));
  foreach($query_results as $result){
    echo $result->post_title;
  }
}

0
C0G1T0 5 months 2022-04-01T13:22:17-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse