admin管理员组文章数量:1026989
I am trying to get a list of posts if has same zipcode values. Thanks in advance for the help.
<?php
$query = new WP_Query( array(
'post_type'=> array('service'),
'posts_per_page' => -1,
'meta_query' => array( array(
'key'=> 'zipcode',
'value'=> ','.$zip.',',
'compare'=> 'LIKE'
) )
));
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<h3><?php the_title(); ?> </h3>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_query(); ?>
<?php else: ?>
No results found.
<?php endif; ?>
I am trying to get a list of posts if has same zipcode values. Thanks in advance for the help.
<?php
$query = new WP_Query( array(
'post_type'=> array('service'),
'posts_per_page' => -1,
'meta_query' => array( array(
'key'=> 'zipcode',
'value'=> ','.$zip.',',
'compare'=> 'LIKE'
) )
));
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<h3><?php the_title(); ?> </h3>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_query(); ?>
<?php else: ?>
No results found.
<?php endif; ?>
Share
Improve this question
edited Mar 27, 2019 at 14:34
fuxia♦
107k39 gold badges255 silver badges459 bronze badges
asked Mar 26, 2019 at 9:09
PraveenPraveen
2405 silver badges19 bronze badges
3
|
2 Answers
Reset to default 2Following code will be proper for the meta query.
$query_args = array(
'post_type' => 'service',
'posts_per_page' => -1,
'meta_query' => array(
array(
'value' => $zip,
'compare' => 'LIKE',
'key' => 'zipcode',
),
)
);
$query = new WP_Query($query_args);
<?php if ( $query->have_posts() ) :while ( $query->have_posts() ) : $query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_query(); ?>
<?php else: ?>
No results found.
<?php endif; ?>
Hope it helps.
This code may help you to get perfect results.
<?php
$query_args = array(
'post_type' => 'service',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key'=> 'zipcode',
'value'=> $zip,
'type' => 'numeric',
'compare'=> '=',
),
)
);
$query = new WP_Query($query_args);
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile;
wp_reset_query();
else: ?>
<h3>No results found.</h3>
<?php endif; ?>
I am trying to get a list of posts if has same zipcode values. Thanks in advance for the help.
<?php
$query = new WP_Query( array(
'post_type'=> array('service'),
'posts_per_page' => -1,
'meta_query' => array( array(
'key'=> 'zipcode',
'value'=> ','.$zip.',',
'compare'=> 'LIKE'
) )
));
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<h3><?php the_title(); ?> </h3>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_query(); ?>
<?php else: ?>
No results found.
<?php endif; ?>
I am trying to get a list of posts if has same zipcode values. Thanks in advance for the help.
<?php
$query = new WP_Query( array(
'post_type'=> array('service'),
'posts_per_page' => -1,
'meta_query' => array( array(
'key'=> 'zipcode',
'value'=> ','.$zip.',',
'compare'=> 'LIKE'
) )
));
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<h3><?php the_title(); ?> </h3>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_query(); ?>
<?php else: ?>
No results found.
<?php endif; ?>
Share
Improve this question
edited Mar 27, 2019 at 14:34
fuxia♦
107k39 gold badges255 silver badges459 bronze badges
asked Mar 26, 2019 at 9:09
PraveenPraveen
2405 silver badges19 bronze badges
3
-
What does the above code do? Is it working? And what do the full values in
zipcode
look like? Can you reformat your code so it's easier to read? Indenting correctly should be enough – Tom J Nowell ♦ Commented Mar 26, 2019 at 9:42 -
@TomJNowell
zipcode
are numbers for example12345
. If posts have value12345
in the custom field. then it should display all posts which have the12345
value. The above code is working fine but displays only one post. – Praveen Commented Mar 26, 2019 at 9:45 - Are those metavalues only zipcodes? Your code block implies it's actually a comma separated list e.g. 12346,67890,etc... – Tom J Nowell ♦ Commented Mar 26, 2019 at 14:23
2 Answers
Reset to default 2Following code will be proper for the meta query.
$query_args = array(
'post_type' => 'service',
'posts_per_page' => -1,
'meta_query' => array(
array(
'value' => $zip,
'compare' => 'LIKE',
'key' => 'zipcode',
),
)
);
$query = new WP_Query($query_args);
<?php if ( $query->have_posts() ) :while ( $query->have_posts() ) : $query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_query(); ?>
<?php else: ?>
No results found.
<?php endif; ?>
Hope it helps.
This code may help you to get perfect results.
<?php
$query_args = array(
'post_type' => 'service',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key'=> 'zipcode',
'value'=> $zip,
'type' => 'numeric',
'compare'=> '=',
),
)
);
$query = new WP_Query($query_args);
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile;
wp_reset_query();
else: ?>
<h3>No results found.</h3>
<?php endif; ?>
本文标签: wp queryGet all Posts If has same custom field values in Posts
版权声明:本文标题:wp query - Get all Posts If has same custom field values in Posts 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745654177a2161498.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
zipcode
look like? Can you reformat your code so it's easier to read? Indenting correctly should be enough – Tom J Nowell ♦ Commented Mar 26, 2019 at 9:42zipcode
are numbers for example12345
. If posts have value12345
in the custom field. then it should display all posts which have the12345
value. The above code is working fine but displays only one post. – Praveen Commented Mar 26, 2019 at 9:45