问题
I have a list of taxonomy terms with checkboxes that post as an array on form submit to a page template called Post List. My goal here is to list taxonomy terms with checkboxes, then on form submit, display the corresponding posts that have the checked terms assigned to them e.g.
<?php
$terms = get_terms("the_taxonomy");
$count = count($terms);
if ( $count > 0 ){?>
?>
<form action="<?php echo esc_url( get_permalink( get_page_by_title( 'Post List' ) ) ); ?>" method="post">
<?php foreach ( $terms as $term ) {?>
<input type="checkbox" name="terms[]" value="<?php echo $term->name ?>" /><?php echo $term->
name ?> <br />
<?php }?>
<input type="submit" value="Submit" />
</form>
When I print the array within Post List page (post-list.php), I do get the returned terms, so I can confirm that it works
<?php
/*
Template Name: Post List
*/
?>
<?php print_r($_POST['terms']);?>
<!---OUTPUT--->
Array ( [0] => term1 [1] => term2 [2] => term3 [3] => term4 )
How could I compare the returned terms array to the stored terms of the custom post type, and return posts based off of which terms were selected and submitted? Thanks for your help.
回答1:
Read the line of stiwdio's question from this link http://wordpress.org/support/topic/exclude-posts-containing-a-particular-tag in which he wrote
$term = get_term_by('slug','helen', 'post_tag');
so you need to pass comma separated terms like this which you are getting on post list template. You can use implode function to get comma separated list like this
$var = "'".implode("','",$_POST['terms'])."'";
echo $var; //OUTPUT - 'term1','term2','term3','term4'
来源:https://stackoverflow.com/questions/12022730/storing-terms-in-post-array-and-returning-corresponding-posts-on-form-submit