Wordpress get previous/next post with meta value not empty

134 views Asked by At

Im trying to build a music player for my site, Im building endpoints (Wordress REST API) that will allow me to get the previous and next post with the a meta value not empty ( meta_val is the soundcloud_id ) I am passing the current ID which the player is using. I cant seem to work out how to get the next and previous post that include a soundcloud_id. This is what I have so far...

Thanks

         $args = array(
            'post_type' => 'track',
            'posts_per_page'=>'1',
            'p' => array($prevID),
            'post_status' => "publish",
            'offset'=> "-1",
            'meta_query' => array(
                array(
                    'key'     => 'soundcloud_id',
                    'value'   => ' ',
                    'compare' => '!=',
                ),
            ),
        );

        $posts_query = new WP_Query();
        $query_result = $posts_query->query( $args );
        $posts = array();

        foreach ( $query_result as $post ) {

            $post->soundcloud_id = get_field('soundcloud_id', $post->ID);
            $posts[] = $post;

        }

        $response = new WP_REST_Response( $post );
        $response->set_status( 200 );
        return $response;
0

There are 0 answers