admin管理员组

文章数量:1294339

I have made a custom page for an university website where all the available scholarships are listed. User can specify city, degree, intake, scholarship type and language to search for universities with available scholarships. For this I've used advanced custom field and custom post type UI plugins and added custom fields to get data from the admin. I've tried to show the data in a tabular form in that page and it is showing the way I want to. But the problem is when i try to use the filter option for any of the above criteria(city, degree, intake, scholarship type and language) I'm getting no results but the table remains the same (The table should show only the filtered data, but it isn't). I tried checking the $args and all the selected parameters are there. I suspect there is some problem with the $query variable because when i checked it had all the posts data. you can check the output results here

here's my code

<?php

/**
 * Template Name: Scholarship Page
 *
 * @package HelloElementor
 */

if (!defined('ABSPATH')) {
  exit; // Exit if accessed directly.
}

get_header();

?>


<div class="container">
  <h4 class="lesson-title">Browse and Apply for Scholarships</h4>
  <form action="#" method="get">

    <select name="city">
      <!-- <option value="">Select city/province</option> -->
      <option value="zhejiang">Zhejiang</option>
      <option value="zhengzhou">Zhengzhou</option>
      <option value="tshinghua">Tshinghua</option>
      <option value="peking">Peking</option>
    </select>

    <select name="scholarship_type">
      <option value="partial">Partial</option>
      <option value="full">Full</option>
    </select>

    <select name="degree">
      <option value="bachelor">Bachelor</option>
      <option value="masters">Masters</option>
      <option value="phd">PHD</option>
    </select>

    <select name="language">
      <option value="">Select Language</option>
      <option value="english">English</option>
      <option value="chinese">Chinese</option>
    </select>

    <select name="intake">
      <option value="">Select Intake</option>
      <option value="summer">Summer</option>
      <option value="winter">Winter</option>
      <option value="autumn">Autumn</option>
    </select>
    <button type="submit" name="">Search</button>
  </form>

<?php
if ($_GET['city'] && !empty($_GET['city'])) {
  $city = $_GET['city'];
}

if ($_GET['scholarship_type'] && !empty($_GET['scholarship_type'])) {
  $scholarship_type = $_GET['scholarship_type'];
}

if ($_GET['degree'] && !empty($_GET['degree'])) {
  $degree = $_GET['degree'];
}

if ($_GET['language'] && !empty($_GET['language'])) {
  $language = $_GET['language'];
}

if ($_GET['intake'] && !empty($_GET['intake'])) {
  $intake = $_GET['intake'];
}
$args = array(
  'post_type' => 'scholarships',
  'posts_per_page' => -1,
  // 'orderby' => 'meta_value',
  'order' => 'ASC',
  'meta_query' => array(
    'relation' => "OR",
    array(
      'key' => 'cityprovince',
      'value' => $city,
      'compare' => '='
    ),

    array(
      'key' => 'scholarship',
      'value' => $scholarship_type,
      'compare' => '='
    ),

    array(
      'key' => 'degree',
      'value' => $degree,
      'compare' => '='
    ),

    array(
      'key' => 'language',
      'value' => $language,
      'compare' => 'LIKE'
    ),

    array(
      'key' => 'intake',
      'value' => $intake,
      'compare' => 'LIKE'
    )
  )
);
  
  $query = new WP_Query($args);
  
  // echo var_dump($args);
  // echo "<br>";
  // echo "<br>";
  ?>
  <div class="post clearfix">
    <table style="width:100%">
      <tr>
        <th>Name</th>
        <th>City/Province</th>
        <th>Major</th>
        <th>Program ID</th>
        <th>Degree</th>
        <th>Language</th>
        <th>Intake</th>
        <th>Scholarship</th>
        <th>Total Amount</th>
      </tr>
      <?php
      while ($query->have_posts()) : $query->the_post();
      //echo var_dump($query);
      ?>
        <tr>
          <td><?php the_title(); ?></td>
          <td><?php the_field('cityprovince'); ?></td>
          <td><?php the_field('major'); ?></td>
          <td><?php the_field('program_id'); ?></td>
          <td><?php the_field('degree'); ?></td>
          <td><?php the_field('language'); ?></td>
          <td><?php the_field('intake'); ?></td>
          <td><?php the_field('scholarship'); ?></td>
          <td><?php the_field('total_amount'); ?></td>
        </tr>
      <?php endwhile;
      wp_reset_query(); ?>
    </table>
  </div>
</div>

<?php get_footer(); ?>

The key in meta_query are the field name in ACF. So any suggestions?

本文标签: phpCustom search query on Wordpress page not working