处理要在get_posts()中使用的JSON解码 [英] Processing JSON decode to use in get_posts()

查看:135
本文介绍了处理要在get_posts()中使用的JSON解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力尝试使用自定义字段中的数据来返回帖子.这是我在functions.php中拥有的函数.我可以返回我的帖子,除了它们不限于$ json变量中定义的帖子.我可以解码json并返回数组...但是我似乎无法以将其正确填充到$ dishes_arg中"posts__in"数组的方式进行转换.

I have been struggling for a couple days with trying to use the data in a custom field to return posts. Here is my function that I have in functions.php. I am able to return my posts, except that they aren't limited to the ones defined in the $json variable. I can decode the json and return the array...but I can't seem to convert it in such a way that it fills in my array properly for the "posts__in" in the $dishes_arg.

有人可以帮助我确定我在做什么错吗?

Can anyone help me identify what I am doing wrong?

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();

}       
}

推荐答案

我想出了...原来,我只需要剪掉一堆妨碍代码的代码:$ include = array(). .. $ args = array()... $ dish_args = array()... $ dishes = get_posts().这是更正的部分:

I figured it out...turns out I just had to cut out a bunch of code that was getting in the way: $include = array() ... $args = array() ... $dish_args = array() ... $dishes = get_posts(). Here is the corrected portion:

    $json = '[{"id":"435"},{"id":"527"},{"id":"563"},{"id":"568"}]';
    $dishes = json_decode($json);

    if ($dishes) {
    foreach ($dishes as $dish) {
// Echo titles and permalinks

    }
    }

这篇关于处理要在get_posts()中使用的JSON解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆