Processing JSON decode to use in get_posts()

2019-09-02 03:40发布

问题:

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

}       
}

回答1:

You jSon property is id not ID

$include[] = $a->ID;

Should be

$include[] = $a->ID;

Where/why is there jSon here? Is this coming from something else. Otherwise that could just be a simple delimitated list or even a normal PHP array.



回答2:

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

    }
    }