我使用此功能通过meta\\u键拉取自定义帖子:
<?php $args = array(
\'numberposts\' => 10,
\'meta_key\' => \'allvotes\',
\'orderby\' => \'meta_value\',
\'order\' => \'DESC\',
\'post_type\' => \'things\',
\'post_status\' => \'publish\' );
$mystuff = get_posts( $args );
?>
我想使用一个表单来编辑我的变量,但当我尝试我的代码时,它不会返回任何帖子。。。
<?php
$filter = $_POST[\'country\'];
$submit = $_POST[\'submit\'];
if(isset($submit)){
$args = array(
\'numberposts\' => 10,
\'meta_key\' => \'$filter\',
\'orderby\' => \'meta_value\',
\'order\' => \'DESC\',
\'post_type\' => \'things\',
\'post_status\' => \'publish\' );
$mystuff = get_posts( $args );
$name = $filter ;
}
else {
$args = array(
\'numberposts\' => 10,
\'meta_key\' => \'canada\',
\'orderby\' => \'meta_value\',
\'order\' => \'DESC\',
\'post_type\' => \'things\',
\'post_status\' => \'publish\' );
$mystuff = get_posts( $args );
$name = \'Everywhere\';
};
?>
最合适的回答,由SO网友:EAMann 整理而成
不要将变量放在单引号中。
这是:
if(isset($submit)){
$args = array(
\'numberposts\' => 10,
\'meta_key\' => \'$filter\',
\'orderby\' => \'meta_value\',
\'order\' => \'DESC\',
\'post_type\' => \'things\',
\'post_status\' => \'publish\' );
$mystuff = get_posts( $args );
$name = $filter ;
}
应该是这样的:
if(isset($submit)){
$args = array(
\'numberposts\' => 10,
\'meta_key\' => $filter,
\'orderby\' => \'meta_value\',
\'order\' => \'DESC\',
\'post_type\' => \'things\',
\'post_status\' => \'publish\' );
$mystuff = get_posts( $args );
$name = $filter ;
}