How can I combine this php statement to get the results of multiple variable inputs?
This is my WordPress query but is not a wordpress related question. It shows the posts that have meta_key as extra1 and meta_value as test
<?php $customkey1 = extra1; ?>
<?php $customvalue1 = test; ?>
<?php query_posts('meta_key=' . $customkey1 . '&meta_value=' . $customvalue1 . ''); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif; ?>
My question is how can I still show the posts that have extra1
as metakey and test
as metavalue but also the posts that have extra2
as metakey and test2
as metavalue in the same query. A combination of two or mor开发者_如何学运维e variables.
So you want to combine the results of two queries? Check this out You may just need to make two query objects and combine them.
Something like (note, I do not have WordPress installed, nor use WordPress. But assuming that its API is not a lie):
<?php
// The Query
$the_query = new WP_Query( $args );
$the_second_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// The Second Loop
while ( $the_second_query->have_posts() ) : $the_second_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
Note: This is hideous, ugly, and will work but in most cases should be considered WRONG by professionals. More elegantly, at least I would use a function to parse the post. Most elegantly, I would do what has been suggested elsewhere, encapsulate the combined query in a MySQL bit. But given the op's knowledge, this seems to be the "best approach" for quickly solving this problem, hopefully only once. (Repeated application of this approach will turn it into a messy nightmare)
精彩评论