admin管理员组

文章数量:1432607

I am trying to get post of custom post type by two meta value but it does not work. I have followed lots of article but not worked below are my code.

Note: 1. first meta value is a type of date eg: '17/12/1976' 2. second meta value is a type of text eg: 'E20001011'

$rollnum = $_POST['rollnum'];
$studentdob = $_POST['studentdob'];

<form class="advanced-search-form" method="post">
   <div class="form-group">
    <label>Student Roll NO.</label>
    <input type="text" name="rollnum" value="<?php echo esc_attr( $rollnum ); ?>">
   </div>
   <div class="form-group">
    <label>Student date of birth</label>
    <input type="text" name="studentdob" value="<?php echo esc_attr( $studentdob ); ?>" placeholder="dd/mm/yyyy">
   </div>
   <div class="form-group">
     <input type="submit" value="Search" style="font-size:18px">
   </div>
</form>             

<?php
if ( $rollnum && $studentdob ): ?>
    <div class="search-result">
     <?php
      $args = array(
        'post_type'  => 'student_results',
        'meta_query' => array(
           array(
             'key'     => 'student_date_of_birth',
             'value'   => $studentdob,
           ),
           array(
             'key'     => 'student_roll_no',
             'value'   => $rollnum,
          ),
        ),
     );           
     $search_query = new WP_Query( $args );

    if ( $search_query->have_posts()):
        while ( $search_query->have_posts() ) {
            $search_query->the_post();
            get_template_part( 'template-parts/content', 'result' );
        }
        wp_reset_postdata();
    ?>
     <?php else: ?>
     <p>No result found.</p>
     <?php endif; ?>
     </div>
    <?php endif; ?>

get_template_part from the file is "template-parts/content-result.php" and code is given below

<article>
<div class="blog-lists">
    <div class="entry-content">
            <?php
            the_title();
            the_content();      
            ?>
    </div>
</div>  
</article>

I am trying to get post of custom post type by two meta value but it does not work. I have followed lots of article but not worked below are my code.

Note: 1. first meta value is a type of date eg: '17/12/1976' 2. second meta value is a type of text eg: 'E20001011'

$rollnum = $_POST['rollnum'];
$studentdob = $_POST['studentdob'];

<form class="advanced-search-form" method="post">
   <div class="form-group">
    <label>Student Roll NO.</label>
    <input type="text" name="rollnum" value="<?php echo esc_attr( $rollnum ); ?>">
   </div>
   <div class="form-group">
    <label>Student date of birth</label>
    <input type="text" name="studentdob" value="<?php echo esc_attr( $studentdob ); ?>" placeholder="dd/mm/yyyy">
   </div>
   <div class="form-group">
     <input type="submit" value="Search" style="font-size:18px">
   </div>
</form>             

<?php
if ( $rollnum && $studentdob ): ?>
    <div class="search-result">
     <?php
      $args = array(
        'post_type'  => 'student_results',
        'meta_query' => array(
           array(
             'key'     => 'student_date_of_birth',
             'value'   => $studentdob,
           ),
           array(
             'key'     => 'student_roll_no',
             'value'   => $rollnum,
          ),
        ),
     );           
     $search_query = new WP_Query( $args );

    if ( $search_query->have_posts()):
        while ( $search_query->have_posts() ) {
            $search_query->the_post();
            get_template_part( 'template-parts/content', 'result' );
        }
        wp_reset_postdata();
    ?>
     <?php else: ?>
     <p>No result found.</p>
     <?php endif; ?>
     </div>
    <?php endif; ?>

get_template_part from the file is "template-parts/content-result.php" and code is given below

<article>
<div class="blog-lists">
    <div class="entry-content">
            <?php
            the_title();
            the_content();      
            ?>
    </div>
</div>  
</article>
Share Improve this question edited Apr 11, 2019 at 8:58 Yajuvendra pratap singh asked Apr 10, 2019 at 21:45 Yajuvendra pratap singhYajuvendra pratap singh 12 bronze badges 10
  • What do you mean by "does not work"? Nothing being displayed? WSOD? What does template-parts/content/result.php contain? – norman.lol Commented Apr 10, 2019 at 21:56
  • Is the form in the same file as the query? – Howard E Commented Apr 10, 2019 at 23:19
  • What is the content of your $search_query? – moped Commented Apr 11, 2019 at 6:17
  • Hi @leymannx, Yes its nothing being displayed and also I have edited my question and given my included template part code please have a look and let me know what did I do wrong? – Yajuvendra pratap singh Commented Apr 11, 2019 at 9:10
  • Hi @HowardE, I have edited my question and provided my whole code of file please have a look – Yajuvendra pratap singh Commented Apr 11, 2019 at 9:15
 |  Show 5 more comments

1 Answer 1

Reset to default 0

I did by removing backslash(/) from Date.

$resultdob = $_POST['studentdob'];
$studentdob = str_replace("/", "", $resultdob);

本文标签: wp queryHow to get post by meta value