我只需要查询自定义帖子类型-即我的WP安装中的所有帖子类型,不包括帖子和页面。我用过get_post_types
要生成我要查询的所有自定义帖子类型的字符串,请执行以下操作:
$args=array(
\'public\' => true,
\'exclude_from_search\' => false,
\'_builtin\' => false
);
$output = \'names\'; // names or objects, note names is the default
$operator = \'and\'; // \'and\' or \'or\'
$post_types=get_post_types($args,$output,$operator);
// Loop thru the cpts and assign a related taxonomy to a variable
$posttypes_array = "";
foreach ($post_types as $post_type ) {
$posttypes_array .= "$post_type, ";
}
$posttypes_array = rtrim($posttypes_array, \', \');
echo $posttypes_array;
变量的输出
$posttypes_array
是否为字符串:
\'puzzles\', \'quizzes\', \'challenges\', \'tales\', \'can_you_help\'
我的问题是,我无法使用此变量查询所有这些帖子类型,如下所示:
$buildArgsAllQuestions = array( // Add out new query parameters
\'post_type\' => array($posttypes_array),
//\'post_type\' => $posttypes_array,
\'orderby\' => \'date\',
\'order\' => \'DESC\',
);
仅当我将此行用于post\\u类型时,我对所有自定义帖子的查询才起作用:
\'post_type\' => array( \'puzzles\', \'quizzes\', \'challenges\', \'tales\', \'can_you_help\' ),
有人能告诉我如何使用这个变量吗
$posttypes_array
或类似于查询所有自定义posttype的内容,如下所示:
\'post_type\' => array($posttypes_array)
?