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
|
Show 5 more comments
1 Answer
Reset to default 0I did by removing backslash(/) from Date.
$resultdob = $_POST['studentdob'];
$studentdob = str_replace("/", "", $resultdob);
本文标签: wp queryHow to get post by meta value
版权声明:本文标题:wp query - How to get post by meta value 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745600819a2665554.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
template-parts/content/result.php
contain? – norman.lol Commented Apr 10, 2019 at 21:56