Custom Fields
Re-Envisioned
Support General Display custom post custom fields everywhere in frontend

  • Creator
    Topic
  • #4841
    sergeysemenov
    Participant

    Hello.
    I purchased your bundle, so I have all tools.
    I created custom fields for custom posts. Now I need to display these fields not only on single-custompost.php, but everywhere in frontend. For example, get values in product taxonomy for each custom post block. Also I created Term Metaboxes with image_advanced and I want them in slider on my homepage. When new term is created and images are attached, they automatically appear on homepage. For now, I figured out how to display these fields by post_id. But I want them update by adding new posts/terms.

    Looking forward to your assistance.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Author
    Replies
  • #4842
    Anh Tran
    Keymaster

    Hi Sergey,

    To get the post meta and show it in the frontend, you can use the rwmb_meta function with the last parameter for the post ID:

    $meta = rwmb_meta( 'field_id', $args, $post_id );
    echo $meta;

    Getting the post ID depends on your (probably custom) query/structure of the website. If you’re using WP_Query class to query posts and use $query->the_post() in the custom loop, then you can omit that parameter, like this:

    $query = new WP_Query( array(
        'post_type' => 'post',
        'posts_per_page' => 3,
    ) );
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();
            
            $meta = rwmb_meta( 'field_id' );
            echo $meta;
            // Other things in the loop that you want to display
        }
    }

    But if you don’t use the similar code, you need to look at your code to get the correct post ID.

    It happens similarly for term meta, but in this case, you need to use get_term_meta() function and it requires the term ID:

    $meta = get_term_meta( $term_id, 'field_id', true );
    echo $meta;

    Again, the term ID depends on how you get the terms. If you’re using get_terms function, then you might want to use this code:

    $terms = get_terms( array(
        'taxonomy' => 'category',
        'number' => 3,
    ) );
    if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
        foreach ( $terms as $term ) {
            $meta = get_term_meta( $term->term_id, 'field_id', true );
            echo $meta;
            // Echo something else
        }
    }

    Hope this helps.

    #4861
    sergeysemenov
    Participant

    Thank you, Anh Tran. I caught the idea. It works.

    One more question about ‘Post’ Field.
    The aim is to make clonable dropdown select in my custom post template admin area to manually select ‘related’ custom posts and retrieve thumbnail and title of these posts in frontend. I managed to create such clonable dropdown field, so I can select my custom posts. But I can’t figure out how to display these selected posts thumbs and titles in frontend. Please help!

    #4866
    Anh Tran
    Keymaster

    Hi Sergey,

    Here is an example of showing related posts:

    #4869
    sergeysemenov
    Participant

    I can’t figure out how to make Post Field clonable. Metabox Builder doesn’t have such option for Post Field. Neither manual integration in functions.php give no result. Where am I wrong?

    My manual functions.php code is

    $meta_boxes[] = array(
            'title'      => 'Related Products',
            'post_types' => 'product',
    		
            'fields' => array(
                array(
    				
    				'id'        => "{$prefix}related_products",
    				'type'      => 'post',
    				'post_type' => 'product',
    				'clone'     => true,
    				
    				
    			),
    			
    			
    			
            ),
        );
    #4870
    sergeysemenov
    Participant

    I managed to make it clonable. The problem was in my database which already has such values (I tested different variants before).
    So, my Post Fields are created in admin, but when I insert second part of code to single-product.php, my page is blank and doesn’t load.

    Tell me, what am I doing wrong?

    I put this to my single-product.php

    <?php add_filter( 'the_content', function ( $content ) {
    	$related_posts = rwmb_meta( 'rw_related_products' );
    	if ( empty( $related_posts ) || ! is_array( $related_posts ) ) {
    		return $content;
    	}
    	// Use output buffering to use the template tags easier.
    	ob_start();
    	global $post;
    	foreach ( $related_posts as $related_post ) {
    		// Setup the post object so we can use template tags.
    		$post = get_post( $related_post );
    		setup_postdata( $post );
    		// Now you can use template tags.
    		?>
    		<a class="related-post" href="<?php the_permalink(); ?>">
    			<?php the_post_thumbnail( 'thumbnail' ); ?>
    			<h2><?php the_title(); ?></h2>
    		</a>
    		<?php
    	}
    	$content .= ob_get_clean();
    	// Reset the post object.
    	wp_reset_postdata();
    	return $content;
    } );
    #4871
    sergeysemenov
    Participant

    I figured it out.

    functions.php

    $meta_boxes[] = array(
            'title'      => 'Related Products',
            'post_types' => 'product',
    		
            'fields' => array(
                array(
    				
    				'id'        => "{$prefix}related_posts",
    				'type'      => 'post',
    				'post_type' => 'product',
    				'clone'     => true,
    				
    			),
    			
            ),
        );

    single-product.php

    <?php 
    	$related_posts = rwmb_meta( 'rw_related_posts' );
    	
    	foreach ( $related_posts as $related_post ) {
    		// Setup the post object so we can use template tags.
    		$post = get_post( $related_post );
    		setup_postdata( $post );
    		// Now you can use template tags.
    		?>
    		
    			
    				">
    				
    				
    				
    			
    			
    				">
    			
    		</div>
    		<?
    	}?>
    	<?php 
    	wp_reset_postdata();
    	?>

    This works for me.

    Thank you for your assistance, Anh Tran.

    #4875
    Anh Tran
    Keymaster

    Glad that you solved the problem! Feel free to open any topic if you have any trouble.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Display custom post custom fields everywhere in frontend’ is closed to new replies.