skip to Main Content

I’m trying to retrieve all post type of every multisite to populate a select from ACF PRO. But when I var_dump receive nothing but. It seems I dont even pass on the while have post. But I know "wphr_hr_recruitment" have like 5 instances, and also this code works on page template.

function getSites(){
        $args = array(
            'network_id' => null,
            'public'     => null,
            'archived'   => null,
            'mature'     => null,
            'spam'       => null,
            'deleted'    => null,
            'limit'      => 100,
            'offset'     => 0,
        ); 
        $sites = wp_get_sites( $args );
        wp_reset_postdata();
        return $sites;
    }
    function acf_load_job( $field ) {
        $jobs=array();
        $currentJobs=array();
        $sites = getSites();
        foreach ( $sites as $site) {
            switch_to_blog(intval($site['blog_id']));
            $query = new WP_Query( [
                'post_type'      => 'wphr_hr_recruitment',
                'post_status' => 'publish',
                'posts_per_page' => -1,
                'order'          => 'DESC',
                'orderby'        => 'post_date'
            ]);
            
            if ( $query->have_posts() ) :
                while ( have_posts()){
                    // $query->next_post();
                    $currentJobs[] = $query->post;
                }                        
            endif;
            wp_reset_postdata();
            restore_current_blog();
        }
        foreach($currentJobs as $currentJob){
            $jobs[$currentJob["ID"]] = $currentJob["post_title"];
        }
        $field['required'] = true;
        $field['choices'] = $jobs;
        return $field;
    }``

The function GetSites retreive all ID so this part seems to work

2

Answers


  1. Chosen as BEST ANSWER

    I found something like this, and it's actually work;

    function getSites(){
            $args = array(
                'network_id' => null,
                'public'     => null,
                'archived'   => null,
                'mature'     => null,
                'spam'       => null,
                'deleted'    => null,
                'limit'      => 100,
                'offset'     => 0,
            ); 
            $sites = wp_get_sites( $args );
            wp_reset_postdata();
            return $sites;
        }
        function acf_load_job( $field ) {
            $jobs=array();
            $sites = getSites();
           
            foreach ( $sites as $site ) {
                switch_to_blog(intval($site['blog_id']));   
                $query = new WP_Query(
                    array(
                        'post_type'      => 'wphr_hr_recruitment',
                        'post_status' => 'publish',
                        'posts_per_page' => -1,
                        'order'          => 'DESC',
                        'orderby'        => 'post_date'
                    )
                );
                
                while ( $query->have_posts() ) {
                    $query->next_post();
                    $expire_date = get_post_meta( $query->post->ID, '_expire_date', true ) ? get_post_meta(  $query->post->ID, '_expire_date', true ) : 'N/A';
                    $today = new DateTime('NOW');
    
                    if($expire_date !='N/A'){
                        $e_date = new DateTime($expire_date);
                        if ( $e_date >= $today ) {
                            $jobs[$query->post->ID] = $query->post->post_title;
                        }
                    }
                    
                }
                restore_current_blog();
            }
          
            $field['required'] = true;
            $field['choices'] = $jobs;
            return $field;
        }
    

  2. Use Network_query

    $args =  [
           'post_type'      => 'wphr_hr_recruitment',
           'post_status' => 'publish',
           'posts_per_page' => -1,
           'order'          => 'DESC',
           'orderby'        => 'post_date'
       ];
      $query_wphr_hr_recruitment = new Network_Query( $args );
      if( $query_wphr_hr_recruitment->have_posts() ) :
         while( $query_wphr_hr_recruitment->have_posts() ) : $query_wphr_hr_recruitment->the_post();
                     switch_to_blog( $query_wphr_hr_recruitment->post->BLOG_ID );
                     echo '<h2>' . get_the_title( $query_wphr_hr_recruitment->post->ID ) . '</h2>';
                     echo get_the_post_thumbnail( $query_wphr_hr_recruitment->post->ID, 'medium' );
                     echo '<time>' . get_the_time( 'j F, Y', $query_wphr_hr_recruitment->post->ID ) . '</time>';
                     echo '<a href="' . get_permalink( $query_wphr_hr_recruitment->post->ID ) . '">Read more</a>';
                     restore_current_blog();
         endwhile;
      else :
         echo '<p>Nothing found for your search criteria.</p>';
      endif;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search