我一直在努力使用自定义字段中的数据来返回帖子。这是我在functions.php中的函数。我可以返回我的帖子,除了它们不限于$ json变量中定义的那些。我可以解码json并返回数组......但我似乎无法以这样的方式转换它,以便它正确填充我的数组中的$ dishes_arg中的“posts__in”。
任何人都可以帮我确定我做错了什么吗?
add_action ('woo_loop_before', 'hgf_home_menu');
function hgf_home_menu () {
if (is_home() || is_front_page()) {
wp_reset_query();
global $posts;
$menu_args = array(
'post_type'=>'single_menu',
'posts_per_page' => 1,
'meta_key' => 'orderby_date',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'orderby_date', // Order-By Date field is upcoming
'value' => date("Y-m-d"),
'compare' => '>='
),
array(
'key' => 'orderby_date', // Order-By Date isn't more than two weeks out
'value' => date("Y-m-d", strtotime( "+2 weeks")),
'compare' => '<='
)
),
);
// Get menu that meets criteria
$menus = new WP_Query( $menu_args );
// Show menu that meets criteria
if ( $menus->have_posts() ) {
while ( $menus->have_posts() ) {
$menus->the_post();
}
wp_reset_postdata();
// Get the menu's product/post listing
$json = '[{"id":"435"},{"id":"527"},{"id":"563"},{"id":"568"}]';
$array = json_decode($json);
$include = array();
foreach($array as $a) {
$include[] = $a->ID;
}
$args = array(
'posts_per_page' => -1,
'include' => $include);
$posts = get_posts($args);
$dish_args = array(
'post_type' => 'product',
'post__in' => $posts,
);
// Get dishes in menu listing
$dishes = get_posts( $dish_args );
if ($dishes) {
foreach ($dishes as $dish) {
}
}
} else { // no posts found }
/* Restore original Post Data */
wp_reset_postdata();
}
}
答案 0 :(得分:0)
你jSon属性是id而不是ID
$include[] = $a->ID;
应该是
$include[] = $a->ID;
这里有什么/为什么有jSon?这是来自别的东西。 否则,这可能只是一个简单的分隔列表,甚至是普通的PHP数组。
答案 1 :(得分:0)
我弄明白了......事实证明我只需要删除一堆阻碍的代码:$ include = array()... $ args = array()... $ dish_args = array()... $ dishes = get_posts()。以下是更正的部分:
$json = '[{"id":"435"},{"id":"527"},{"id":"563"},{"id":"568"}]';
$dishes = json_decode($json);
if ($dishes) {
foreach ($dishes as $dish) {
// Echo titles and permalinks
}
}