Support

Account

Home Forums General Issues Use of get_field in multisite

Solved

Use of get_field in multisite

  • Hello guys, I’m hitting me here with a question about the use of multi-site plugin.

    First, it is possible that when creating the custom fields on the main site of the network, either craido on all other websites in the network at the same time?

    Another question is how to get values of images of all the network sites within a foreach in the main site.

    I’m making a widget where do I get the latest posts from all network sites (custom post type), however I can’t return the image of post, because even I setting the field with the image id, post object, image url, it always returns a numeric value, example “77”. No returns the URL of the image of that particular post network Web site.

    Already tried using, and get_post_meta get_field, it only works with text fields, does not return the expected value in image fields.

    Can someone help me? Sorry my bad English

    My code is:

    
    function imoveisRede() {
            global $wpdb;
    
            $blogs = $wpdb->get_results( "SELECT blog_id FROM {$wpdb->blogs} WHERE blog_id != {$wpdb->blogid} AND site_id = '{$wpdb->siteid}' AND spam = '0' AND deleted = '0' AND archived = '0' order by blog_id", ARRAY_A);
            array_unshift($blogs, 1); /*Including parent blog in the array*/
            $html = '<div>';
            foreach($blogs as $blog) {
                switch_to_blog( $blog[ 'blog_id' ] );
                $posts = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}posts WHERE post_type='imoveis' AND post_status='publish' ORDER BY RAND() DESC LIMIT 10");
                $count = 0;
                foreach($posts as $post) {
    
                    $id            = $post->ID;
                    $imovelTipo    = get_post_meta($post->ID, 'tipo', true);
                    $br            = get_post_meta($post->ID, 'bairro', true);
                    $cd            = get_post_meta($post->ID, 'cidade', true);
                    $ar            = get_post_meta($post->ID, 'area', true);
                    $bed           = get_post_meta($post->ID, 'dormitorios', true);
                    $bath          = get_post_meta($post->ID, 'banheiros', true);
                    $preco         = get_post_meta($post->ID, 'preco', true);
                    $combinar      = get_post_meta($post->ID, 'tipo_de_valor', true);
                    $estado        = get_post_meta($post->ID, 'estado', true);
                    $mensal        = get_post_meta($post->ID, 'negociacao', true);
                    $gallery       = get_post_meta($post->ID, 'portalImage', true); //this field is type gallery
                    $alt           = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
    
                    $count++;
    
                    /*echo "<pre>";
                    print_r($foto);
                    echo "</pre>";
                    echo "</br>";
                     */
    
                    if ($count == 4) {
                    $html .= '<div class="clear"></div>';
                    }else {
    
                    $html .= '<div class="property span3" id="destaques">';
    
                    $html .='<div class="image">';
    
                    $html .='<div class="content">';
                        $html .='<a href="'.get_permalink($post->ID).'" title="'.$post->post_title.'"></a>';
    
                        $html .='<div class="content">';
                        $html .='<a href="'.the_permalink().'" title="'.$post->post_title.'"></a>';
                        if($gallery) {
                        $html .='<img src="'.$gallery [url].'" alt="'.$alt.'" />';
                        }else {
                        $html .='<img src="'.bloginfo('template_directory').'/images/img_not_available.png" alt=""  />';
                        }
                        $html .='</div>';
                        }
                    $html .='</div>';
    
                }
            }
            $html .= '</div>';
            return $html;
    }
    
  • Hi @websul

    The get_field function will work for you. Perhaps you forgot to use the $post_id parameter?

    If the image field is ‘portalImage’, then change:

    
    $gallery       = get_post_meta($post->ID, 'portalImage', true); //this field is type gallery
    

    to

    
    $gallery       = get_field('portalImage', $post->ID); //this field is type gallery
    

    Your next issues is that your comment mentions this is a gallery field, not an image field. Is this correct?

    If so, you need to update your code to loop through the gallery images. Please refer to the docs for this.

    Thanks
    E

  • @elliot Thanks for reply!

    But dont work for me in multisite:/

    I use

    
    $galerias = get_field('gallery', $post->ID);
    
    if( $galerias ):
    foreach( $galerias as $galeria ):
    $html .='<li style="text-align:center !important;">';
    $html .='<a href="'.get_permalink($post->ID).'" rel="lightbox">
    <img src="'.$galeria['sizes']['large'].'" alt="'.$img_alt.'" /></a>';
    $html .='</li>';
    endforeach;
    endif;
    
  • Full script

    
    function imoveisRede() {
            global $wpdb;
    
            $blogs = $wpdb->get_results( "SELECT blog_id FROM {$wpdb->blogs} WHERE blog_id != {$wpdb->blogid} AND site_id = '{$wpdb->siteid}' AND spam = '0' AND deleted = '0' AND archived = '0' order by blog_id", ARRAY_A);
            array_unshift($blogs, 1); /*Including parent blog in the array*/
            $html = '<div>';
            foreach($blogs as $blog) {
                switch_to_blog( $blog[ 'blog_id' ] );
                $posts = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}posts WHERE post_type='imoveis' AND post_status='publish' ORDER BY RAND() DESC LIMIT 10");
                $count = 0;
                foreach($posts as $post) {
    
                    $galerias = get_field('gallery', $post->ID);
    
                    $count++;
    
                    if ($count == 4) {
                    $html .= '<div class="clear"></div>';
                    }else {
    
                    $html .= '<div class="property span3" id="destaques">';
    
                    $html .='<div class="image">';
    
                    $html .='<div class="content">';
                        $html .='<a href="'.get_permalink($post->ID).'" title="'.$post->post_title.'"></a>';
    
                        $html .='<div class="content">';
                        $html .='<a href="'.the_permalink().'" title="'.$post->post_title.'"></a>';
                        if( $galerias ):
    foreach( $galerias as $galeria ):
    $html .='<li style="text-align:center !important;">';
    $html .='<a href="'.get_permalink($post->ID).'" rel="lightbox">
    <img src="'.$galeria['sizes']['large'].'" alt="'.$img_alt.'" /></a>';
    $html .='</li>';
    endforeach;
    endif;
                        }
                        $html .='</div>';
                        }
                    $html .='</div>';
    
                }
            }
            $html .= '</div>';
            return $html;
    }
    
  • Hi @websul

    Your code is extremely custom. Have you debugged it line by line and can confirm each line of code is working as expected?

    Thanks
    E

  • Hi @elliot, thanks for reply again.

    So just a thought.

    $images = get_field('gallery-field', $post->ID);
    
    if( $images ):
    foreach( $images as $image ):
    echo '<li style="text-align:center !important;">';
    echo '<a href="'.get_permalink($post->ID).'">';
    echo '<img src="'.$image['sizes']['large'].'" alt="'.$img_alt.'" />';
    echo '</a>';
    echo '</li>';
    endforeach;
    endif;
    

    This form would be the correct way? For that he pull the first image from the Gallery in multsite?

    
    $image['sizes']['large']
    
  • Hi @websul

    Your latest snippet of code will not show 1 image, it will loop through and show all images.

    Does that answer your question?

    Thanks
    E

  • Ok @elliot, thanks, sorry for being so boring

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Use of get_field in multisite’ is closed to new replies.