WordPress get_post_meta not work in loop
-
Hello,
I can not get_post_meta to work inside a loop. Here is the code:<div class="container"> <div class="row"> <!-- Query Post Portfolio--> <?php $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(); $wp_query->query('posts_per_page=9' . '&paged='.$paged); while ($wp_query->have_posts()) : $wp_query->the_post(); ?> <div class="col-lg-4 <?php // Retrieves the stored value from the database $meta_value = get_post_meta( get_the_ID() , 'meta-select', true ); // Checks and displays the retrieved value if( !empty( $meta_value ) ) { echo $meta_value; } ?> col-xs-12" data-aos="fade-up" data-aos-duration="1500" data-aos-easing="ease-out-cubic"> <?php get_template_part( 'template-parts/content','post-portfolio-all-standar', get_post_type() );?> </div> <?php endwhile; wp_reset_postdata(); ?> </div> </div>
where am I wrong? I checked the output, and the function always prints the first option, and the other options even if selected are not printed. Thank you so much
-
This topic was modified 6 years, 4 months ago by
Franchi Web Design.
-
This topic was modified 6 years, 4 months ago by
-
First off, don’t overwrite wp_query. It’s a global variable that shouldn’t be overwritten like this. You can assign a new WP_Query to any variable like so:
$portfolio = new WP_Query( array( 'posts_per_page' => 9, 'paged' => $paged, ) ); if( $portfolio->have_posts() ) { while( $portfolio->have_posts() ) { $portfolio->the_post(); $meta_value = get_post_meta( get_the_ID() , 'meta-select', true ); } // Very Important wp_reset_postdata(); }
I suspect if you switch out what you’re currently doing with the query with what I’ve done above it will start working. Do note the
wp_reset_postdata()
inside the conditional but outside the loop – it resets theglobal $post
so you do not run into any issues down the link.For more examples and information I suggest checking out The Codex on WP_Query
Hello, thanks for a quick reply.
I tried to do as described,this works, but in this way does not print the posts, but only prints the title and the image of the page “Portfolio” in which I find myself.<div class="container"> <div class="row"> <!-- Query Post Portfolio--> <?php $portfolio = new WP_Query( array( 'posts_per_page' => 9, 'paged' => $paged, ) ); if( $portfolio->have_posts() ) { while( $portfolio->have_posts() ) { $portfolio->the_post(); $meta_value = get_post_meta( get_the_ID() , 'meta-select', true ); } // Very Important wp_reset_postdata(); } ?> <div class="col-lg-4 <?php // Retrieves the stored value from the database $meta_value = get_post_meta( get_the_ID() , 'meta-select', true ); // Checks and displays the retrieved value if( !empty( $meta_value ) ) { echo $meta_value; } ?> col-xs-12" data-aos="fade-up" data-aos-duration="1500" data-aos-easing="ease-out-cubic"> <?php get_template_part( 'template-parts/content','post-portfolio-all-standar', get_post_type() );?> </div> </div> </div>
If instead edited like this:
<div class="container"> <div class="row"> <!-- Query Post Portfolio--> <?php $portfolio = new WP_Query( array( 'posts_per_page' => 9, 'paged' => $paged, ) ); if( $portfolio->have_posts() ) { while( $portfolio->have_posts() ) { $portfolio->the_post(); $meta_value = get_post_meta( get_the_ID() , 'meta-select', true ); ?> <div class="col-lg-4 <?php if( !empty( $meta_value ) ) { echo $meta_value; } ?> col-xs-12" data-aos="fade-up" data-aos-duration="1500" data-aos-easing="ease-out-cubic"> <?php get_template_part( 'template-parts/content','post-portfolio-all-standar', get_post_type() );?> </div> <?php } // Very Important wp_reset_postdata(); } ?> </div> </div>
print the posts but the get_post_meta does not work.
this is the meta box code
/* ------------------------------------------------------------------------- * ## 1 Add meta box page Portfolio Standar */ /* ------------------------------------------------------------------------- */ function willer_custom_meta_portfolio_standar() { add_meta_box( 'page-portfolio-standar-meta-box', __( 'Meta box Portfolio Standar', 'willer' ), 'willer_portfolio_standar_meta_callback', 'page' ); } add_action( 'add_meta_boxes', 'willer_custom_meta_portfolio_standar' ); /** Outputs the content of the meta box */ function willer_portfolio_standar_meta_callback( $post ) { wp_nonce_field( basename( __FILE__ ), 'willer_nonce' ); $wl_portfolio_standar = get_post_meta( $post->ID ); $wl_portfolio = get_post_meta( $post->ID ); ?> <div id="wl_template_page_portfolio_standar_wrapper"> <p> <span class="willer-title-meta-box"><?php esc_html_e( 'Enable breadcrumb', 'willer' )?></span> <div class="willer-content-meta-box"> <label for="willer_enable_breadcrumb_portfolio_standar"> <input type="checkbox" name="willer_enable_breadcrumb_portfolio_standar" id="willer_enable_breadcrumb_portfolio_standar" value="yes" <?php if ( isset ( $wl_portfolio_standar['willer_enable_breadcrumb_portfolio_standar'] ) ) checked( $wl_portfolio_standar['willer_enable_breadcrumb_portfolio_standar'][0], 'yes' ); ?> /> <?php _e( 'Checkbox label', 'prfx-textdomain' )?> </label> </div> </p> <p> <label for="meta-select" class="prfx-row-title"><?php _e( 'Example Select Input', 'prfx-textdomain' )?></label> <select name="meta-select" id="meta-select"> <option value="select-one" <?php if ( isset ( $wl_portfolio['meta-select'] ) ) selected( $wl_portfolio['meta-select'][0], 'select-one' ); ?>><?php _e( 'One', 'prfx-textdomain' )?></option>'; <option value="select-two" <?php if ( isset ( $wl_portfolio['meta-select'] ) ) selected( $wl_portfolio['meta-select'][0], 'select-two' ); ?>><?php _e( 'Two', 'prfx-textdomain' )?></option>'; </select> </p> </div> <?php } /** Saves the custom meta input */ function willer_portfolio_standar_meta_save( $post_id ) { // Checks save status $is_autosave = wp_is_post_autosave( $post_id ); $is_revision = wp_is_post_revision( $post_id ); $is_valid_nonce = ( isset( $_POST[ 'willer_nonce' ] ) && wp_verify_nonce( $_POST[ 'willer_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false'; // Exits script depending on save status if ( $is_autosave || $is_revision || !$is_valid_nonce ) { return; } // Checks for input and saves if( isset( $_POST[ 'willer_enable_breadcrumb_portfolio_standar' ] ) ) { update_post_meta( $post_id, 'willer_enable_breadcrumb_portfolio_standar', 'yes' ); } else { update_post_meta( $post_id, 'willer_enable_breadcrumb_portfolio_standar', '' ); } // Checks for input and saves if needed if( isset( $_POST[ 'meta-select' ] ) ) { update_post_meta( $post_id, 'meta-select', $_POST[ 'meta-select' ] ); } } add_action( 'save_post', 'willer_portfolio_standar_meta_save' );
-
This reply was modified 6 years, 4 months ago by
Franchi Web Design.
-
This reply was modified 6 years, 4 months ago by
Franchi Web Design.
It’s difficult to say. You’ll have to do some verifying.
1) Verify that
get_the_ID()
is returning the expected ID
2) Verify that postmeta is being saved in the postmeta table and is assigned to the expected ID ( you can do this in the database or outside this loop manually ).Maybe calling
global $post
at the very top of the file, but I doubt it.printf( '<pre>%1$s</pre>', print_r( $meta_value, 1 ) ); die( 'END' );
The above will print out the value of
$meta_value
( and also kill the page ) just to ensure it’s actually empty.I did the checks and everything seems ok.
get_the_ID()
out of the loop works perfectly, and the value is saved correctly.
TheID
is printed in a post with the correct call, while in the other posts with the value1
.<div class="col-lg-4 select-one col-xs-12 aos-init aos-animate" data-aos="fade-up" data-aos-duration="1500" data-aos-easing="ease-out-cubic"> <article id="post-353" class="post-353 post type-post status-publish format-standard has-post-thumbnail hentry category-uncategorized even"> <a class="willer-img-portfolio-all-standar" href="https://willer.local/test/"> <div class="inside"> <div class="details"> <div class="text"> <h2>test</h2> <div class="wiler-divide-portfolio-page-text-about"></div> </div> </div> <div class="overlay"></div> <img src="https://willer.local/wp-content/uploads/2018/09/aboutme.jpg"> </div> </a> </article> </div>
<div class="col-lg-4 1 col-xs-12 aos-init aos-animate" data-aos="fade-up" data-aos-duration="1500" data-aos-easing="ease-out-cubic"> <article id="post-235" class="post-235 post type-post status-publish format-standard has-post-thumbnail hentry category-vcard odd"> <a class="willer-img-portfolio-all-standar" href="https://willer.local/v-card/"> <div class="inside"> <div class="details"> <div class="text"> <h2>V-Card</h2> <div class="wiler-divide-portfolio-page-text-about"></div> </div> </div> <div class="overlay"></div> <img src="https://willer.local/wp-content/uploads/2018/09/actrat.jpg"> </div> </a> </article> </div>
Value ( select-one ) it’s correct.
Value (1) it’s incorrect.-
This reply was modified 6 years, 4 months ago by
Franchi Web Design.
Your post meta call:
get_post_meta( get_the_ID() , 'meta-select', true );
That third parameter manages whether the function returns a single value or all matching values. Changetrue
tofalse
to get all.Later on, you echo out the result. In the case of multiple values, you cannot simply echo out. Start with
print_r( $meta_value );
to see what data you are working with. You can then refine your output code to better present the data returned.I modified as described:
<div class="container"> <div class="row"> <!-- Query Post Portfolio--> <?php $portfolio = new WP_Query( array( 'posts_per_page' => 3, 'paged' => $paged, ) ); if( $portfolio->have_posts() ) { while( $portfolio->have_posts() ) { $portfolio->the_post(); $meta_value = get_post_meta( get_the_ID() , 'meta-select', false ); ?> <div class="col-lg-4 <?php if( !empty( $meta_value ) ) { print_r( $meta_value ); } ?> col-xs-12" data-aos="fade-up" data-aos-duration="1500" data-aos-easing="ease-out-cubic"> <?php get_template_part( 'template-parts/content','post-portfolio-all-standar', get_post_type() );?> </div> <?php } // Very Important wp_reset_postdata(); } ?> </div> </div>
but it does not work.
This is what he prints<div class="container"> <div class="row"> <!-- Query Post Portfolio--> <div class="col-lg-4 col-xs-12" data-aos="fade-up" data-aos-duration="1500" data-aos-easing="ease-out-cubic"> <article id="post-382" class="post-382 post type-post status-publish format-standard has-post-thumbnail hentry category-uncategorized odd"> <a class="willer-img-portfolio-all-standar" href="https://willer.local/project-standar/" > <div class="inside"> <div class="details"> <div class="text"> <h2>Project standar</h2> <div class="wiler-divide-portfolio-page-text-about"></div> </div> </div> <div class="overlay"></div> <img src="https://willer.local/wp-content/uploads/2018/09/aboutme.jpg"/> </div> </a> </article> </div> <div class="col-lg-4 col-xs-12" data-aos="fade-up" data-aos-duration="1500" data-aos-easing="ease-out-cubic"> <article id="post-380" class="post-380 post type-post status-publish format-standard has-post-thumbnail hentry category-uncategorized even"> <a class="willer-img-portfolio-all-standar" href="https://willer.local/project-1/" > <div class="inside"> <div class="details"> <div class="text"> <h2>Project 1</h2> <div class="wiler-divide-portfolio-page-text-about"></div> </div> </div> <div class="overlay"></div> <img src="https://willer.local/wp-content/uploads/2018/09/test44-1400x800.jpg"/> </div> </a> </article> </div> <div class="col-lg-4 Array ( [0] => select-one ) col-xs-12" data-aos="fade-up" data-aos-duration="1500" data-aos-easing="ease-out-cubic"> <article id="post-353" class="post-353 post type-post status-publish format-standard has-post-thumbnail hentry category-uncategorized odd"> <a class="willer-img-portfolio-all-standar" href="https://willer.local/test/" > <div class="inside"> <div class="details"> <div class="text"> <h2>test</h2> <div class="wiler-divide-portfolio-page-text-about"></div> </div> </div> <div class="overlay"></div> <img src="https://willer.local/wp-content/uploads/2018/09/aboutme.jpg"/> </div> </a> </article> </div> </div> </div>
It looks like “select-one” is the only value saved in meta for that post. Maybe the problem is the code saving values? Overwriting instead of adding?
The code saving values it’s correct because out of the loop function very well.
The code values it’s an imput select(select-one and select-two).
The itself function used in
`<div class=”container”>
<div class=”row”>
<!– Query Post Portfolio–>`
In
container
has worked good.-
This reply was modified 6 years, 4 months ago by
Franchi Web Design.
-
This reply was modified 6 years, 4 months ago by
Franchi Web Design.
-
This reply was modified 6 years, 4 months ago by
Franchi Web Design.
Something is wrong somewhere else, using the same code on my site, changing only the meta_key in order to work with my data, the getting of multiple meta values works as expected. Multiple select form fields are notorious for only saving the last value clicked instead of all ctrl-clicked items, unless properly named and coded. Would you please share the form field HTML and the related saving code?
There’s one way to determine for sure if the data is properly saved. Look at your DB using phpMyAdmin. Navigate to your site’s DB and to the postmeta table. Use the search tab to find all fields where the post_ID is 380 and meta_key is meta-select. I think you will only get one record, though there should be two by your own account.
If you do find there are indeed two, then there must be some plugin or theme conflict. If you were to deactivate all plugins and switch to the twentyseventeen theme, then temporarily add the following code to a twentyseventeen template, you should see the function works correctly.
<?php print_r( get_post_meta( 380, 'meta-select', false )); ?>
Here is the html code of the template:
<?php /** * Template Name: Portfolio Standar * Template Post Type: page * * @author Denis Franchi * @package Willer * @version 1.0.0 * */ get_header();?> <div class="container"> <div class="row"> <!-- Query Post Portfolio--> <?php $portfolio = new WP_Query( array( 'posts_per_page' => 3, 'paged' => get_query_var( 'paged' ) ) ); if( $portfolio->have_posts() ) { while( $portfolio->have_posts() ) { $portfolio->the_post(); $meta_value = get_post_meta( get_the_ID() , 'meta-select', false ); ?> <div class="col-lg-4 <?php if( !empty( $meta_value ) ) { print_r( $meta_value ); } ?> col-xs-12" data-aos="fade-up" data-aos-duration="1500" data-aos-easing="ease-out-cubic"> <?php get_template_part( 'template-parts/content','post-portfolio-all-standar', get_post_type() );?> </div> <?php } // Very Important wp_reset_postdata(); } ?> </div> </div> <!-- Loader oder post --> <div class="container willer-load-more-posts"> <?php $next_link = get_previous_posts_link(false); $prev_link = get_next_posts_link( false); if (get_previous_posts_link('Pagina precedente', $portfolio->max_num_pages )!=$next_link): ?> <button data-animation="animated fadeInRight" class="btn btn-1 btn-border-willer btn-1c"> <?php previous_posts_link( 'Pagina successiva', $portfolio->max_num_pages );?> </button> <?php endif; if (get_next_posts_link('Pagina successiva', $portfolio->max_num_pages)!=$prev_link): ?> <button data-animation="animated fadeInRight" class="btn btn-1 btn-border-willer btn-1c"> <?php next_posts_link( 'Pagina precedente', $portfolio->max_num_pages );?> </button> <?php endif; ?> </div> <?php get_footer(); ?>
<?php /** * content-post-portfolio-all-standar.php * * @author Denis Franchi * @package Willer * @version 1.0.0 */ ?> <?php $willer_image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'willer_big');?> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <a class="willer-img-portfolio-all-standar" href="<?php the_permalink();?>" > <div class="inside"> <div class="details"> <div class="text"> <h2><?php the_title();?></h2> <div class="wiler-divide-portfolio-page-text-about"></div> </div> </div> <div class="overlay"></div> <img src="<?php if ( $willer_image_attributes[0] ) : echo esc_url($willer_image_attributes[0]); else: echo esc_url(get_template_directory_uri()).'/images/willer-default.jpg'; endif; ?>"/> </div> </a> </article>
This is saving code:
/* ------------------------------------------------------------------------- * ## 1 Add meta box page Portfolio Standar */ /* ------------------------------------------------------------------------- */ function willer_custom_meta_portfolio_standar() { add_meta_box( 'page-portfolio-standar-meta-box', __( 'Meta box Portfolio Standar', 'willer' ), 'willer_portfolio_standar_meta_callback', 'page' ); } add_action( 'add_meta_boxes', 'willer_custom_meta_portfolio_standar' ); /** Outputs the content of the meta box */ function willer_portfolio_standar_meta_callback( $post ) { wp_nonce_field( basename( __FILE__ ), 'willer_nonce' ); $wl_portfolio = get_post_meta( $post->ID ); ?> <div id="wl_template_page_portfolio_standar_wrapper"> <p> <label for="meta-select" class="prfx-row-title"><?php _e( 'Example Select Input', 'prfx-textdomain' )?></label> <select name="meta-select" id="meta-select"> <option value="select-one" <?php if ( isset ( $wl_portfolio['meta-select'] ) ) selected( $wl_portfolio['meta-select'][0], 'select-one' ); ?>><?php _e( 'One', 'prfx-textdomain' )?></option>'; <option value="select-two" <?php if ( isset ( $wl_portfolio['meta-select'] ) ) selected( $wl_portfolio['meta-select'][0], 'select-two' ); ?>><?php _e( 'Two', 'prfx-textdomain' )?></option>'; </select> </p> </div> <?php } /** Saves the custom meta input */ function willer_portfolio_standar_meta_save( $post_id ) { // Checks save status $is_autosave = wp_is_post_autosave( $post_id ); $is_revision = wp_is_post_revision( $post_id ); $is_valid_nonce = ( isset( $_POST[ 'willer_nonce' ] ) && wp_verify_nonce( $_POST[ 'willer_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false'; // Exits script depending on save status if ( $is_autosave || $is_revision || !$is_valid_nonce ) { return; } // Checks for input and saves if needed if( isset( $_POST[ 'meta-select' ] ) ) { update_post_meta( $post_id, 'meta-select', $_POST[ 'meta-select' ] ); } } add_action( 'save_post', 'willer_portfolio_standar_meta_save' );
I did the search in the DB as shown with post_ID 380 and meta_key met-select , the result is No rows.
I have also disabled active plugins: Theme Sniffer,Contact Form 7 end WP ULike , but the result does not change.
I put the code in the theme Twentyseventeen the result is always the same.
Putting the code like this instead in Twentyseventeen<?php /** * Template Name: Portfolio Standar * Template Post Type: page * * @author Denis Franchi * @package Willer * @version 1.0.0 * */ get_header();?> <div class="container"> <div class="row"> <!-- Query Post Portfolio--> <?php $portfolio = new WP_Query( array( 'posts_per_page' => 3, 'paged' => get_query_var( 'paged' ) ) ); if( $portfolio->have_posts() ) { while( $portfolio->have_posts() ) { $portfolio->the_post(); ?> <div class="col-lg-4 <?php print_r( get_post_meta( 380, 'meta-select', false )); ?> col-xs-12" data-aos="fade-up" data-aos-duration="1500" data-aos-easing="ease-out-cubic"> <?php get_template_part( 'template-parts/content','post-portfolio-all-standar', get_post_type() );?> </div> <?php } // Very Important wp_reset_postdata(); } ?> </div> </div> <!-- Loader oder post --> <div class="container willer-load-more-posts"> <?php $next_link = get_previous_posts_link(false); $prev_link = get_next_posts_link( false); if (get_previous_posts_link('Pagina precedente', $portfolio->max_num_pages )!=$next_link): ?> <button data-animation="animated fadeInRight" class="btn btn-1 btn-border-willer btn-1c"> <?php previous_posts_link( 'Pagina successiva', $portfolio->max_num_pages );?> </button> <?php endif; if (get_next_posts_link('Pagina successiva', $portfolio->max_num_pages)!=$prev_link): ?> <button data-animation="animated fadeInRight" class="btn btn-1 btn-border-willer btn-1c"> <?php next_posts_link( 'Pagina precedente', $portfolio->max_num_pages );?> </button> <?php endif; ?> </div> <?php get_footer(); ?>
the result is this:
<div class="container"> <div class="row"> <!-- Query Post Portfolio--> <div class="col-lg-4 Array ( ) col-xs-12" data-aos="fade-up" data-aos-duration="1500" data-aos-easing="ease-out-cubic"> </div> <div class="col-lg-4 Array ( ) col-xs-12" data-aos="fade-up" data-aos-duration="1500" data-aos-easing="ease-out-cubic"> </div> <div class="col-lg-4 Array ( ) col-xs-12" data-aos="fade-up" data-aos-duration="1500" data-aos-easing="ease-out-cubic"> </div> </div> </div>
I did another test, with the input option, post_get_meta it works well
<?php /** * Template Name: Portfolio Standar * Template Post Type: page * * @author Denis Franchi * @package Willer * @version 1.0.0 * */ get_header();?> <?php $wl_breadcrumb_portfolio_standar = get_post_meta( get_the_ID(), 'willer_enable_breadcrumb_portfolio_standar', true );?> <div class="container"> <div class="row"> <!-- Query Post Portfolio--> <?php $portfolio = new WP_Query( array( 'posts_per_page' => 3, 'paged' => get_query_var( 'paged' ) ) ); if( $portfolio->have_posts() ) { while( $portfolio->have_posts() ) { $portfolio->the_post(); $meta_value = get_post_meta( get_the_ID() , 'meta-select', false ); ?> <div class="col-lg-4 <?php if( !empty( $meta_value ) ) { print_r( $meta_value ); } ?> col-xs-12" data-aos="fade-up" data-aos-duration="1500" data-aos-easing="ease-out-cubic"> <?php get_template_part( 'template-parts/content','post-portfolio-all-standar', get_post_type() );?> </div> <!-- Function dinamic template enable Breadcrumb --> <?php if ( ! empty( $wl_breadcrumb_portfolio_standar ) ) : ?> <div class="container-fluid willer-container-breadcrumb"> <div class="row"> <div class="willer-img-breadcrumb-page breadcrumb-portfolio-standar"> <?php willer_page_the_breadcrumb(); ?> </div> </div> </div> <!-- End Function dinamic template enable Breadcrumb --> <?php endif; ?> <?php } // Very Important wp_reset_postdata(); } ?> </div> </div> <!-- Loader oder post --> <div class="container willer-load-more-posts"> <?php $next_link = get_previous_posts_link(false); $prev_link = get_next_posts_link( false); if (get_previous_posts_link('Pagina precedente', $portfolio->max_num_pages )!=$next_link): ?> <button data-animation="animated fadeInRight" class="btn btn-1 btn-border-willer btn-1c"> <?php previous_posts_link( 'Pagina successiva', $portfolio->max_num_pages );?> </button> <?php endif; if (get_next_posts_link('Pagina successiva', $portfolio->max_num_pages)!=$prev_link): ?> <button data-animation="animated fadeInRight" class="btn btn-1 btn-border-willer btn-1c"> <?php next_posts_link( 'Pagina precedente', $portfolio->max_num_pages );?> </button> <?php endif; ?> </div> <?php get_footer(); ?>
/* ------------------------------------------------------------------------- * ## 1 Add meta box page Portfolio Standar */ /* ------------------------------------------------------------------------- */ function willer_custom_meta_portfolio_standar() { add_meta_box( 'page-portfolio-standar-meta-box', __( 'Meta box Portfolio Standar', 'willer' ), 'willer_portfolio_standar_meta_callback', 'page' ); } add_action( 'add_meta_boxes', 'willer_custom_meta_portfolio_standar' ); /** Outputs the content of the meta box */ function willer_portfolio_standar_meta_callback( $post ) { wp_nonce_field( basename( __FILE__ ), 'willer_nonce' ); $wl_portfolio_standar = get_post_meta( $post->ID ); $wl_portfolio = get_post_meta( $post->ID ); ?> <div id="wl_template_page_portfolio_standar_wrapper"> <p> <span class="willer-title-meta-box"><?php esc_html_e( 'Enable breadcrumb', 'willer' )?></span> <div class="willer-content-meta-box"> <label for="willer_enable_breadcrumb_portfolio_standar"> <input type="checkbox" name="willer_enable_breadcrumb_portfolio_standar" id="willer_enable_breadcrumb_portfolio_standar" value="yes" <?php if ( isset ( $wl_portfolio_standar['willer_enable_breadcrumb_portfolio_standar'] ) ) checked( $wl_portfolio_standar['willer_enable_breadcrumb_portfolio_standar'][0], 'yes' ); ?> /> <?php _e( 'Checkbox label', 'prfx-textdomain' )?> </label> </div> </p> <p> <label for="meta-select" class="prfx-row-title"><?php _e( 'Example Select Input', 'prfx-textdomain' )?></label> <select name="meta-select" id="meta-select"> <option value="select-one" <?php if ( isset ( $wl_portfolio['meta-select'] ) ) selected( $wl_portfolio['meta-select'][0], 'select-one' ); ?>><?php _e( 'One', 'prfx-textdomain' )?></option>'; <option value="select-two" <?php if ( isset ( $wl_portfolio['meta-select'] ) ) selected( $wl_portfolio['meta-select'][0], 'select-two' ); ?>><?php _e( 'Two', 'prfx-textdomain' )?></option>'; </select> </p> </div> <?php } /** Saves the custom meta input */ function willer_portfolio_standar_meta_save( $post_id ) { // Checks save status $is_autosave = wp_is_post_autosave( $post_id ); $is_revision = wp_is_post_revision( $post_id ); $is_valid_nonce = ( isset( $_POST[ 'willer_nonce' ] ) && wp_verify_nonce( $_POST[ 'willer_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false'; // Exits script depending on save status if ( $is_autosave || $is_revision || !$is_valid_nonce ) { return; } // Checks for input and saves if( isset( $_POST[ 'willer_enable_breadcrumb_portfolio_standar' ] ) ) { update_post_meta( $post_id, 'willer_enable_breadcrumb_portfolio_standar', 'yes' ); } else { update_post_meta( $post_id, 'willer_enable_breadcrumb_portfolio_standar', '' ); } // Checks for input and saves if needed if( isset( $_POST[ 'meta-select' ] ) ) { update_post_meta( $post_id, 'meta-select', $_POST[ 'meta-select' ] ); } } add_action( 'save_post', 'willer_portfolio_standar_meta_save' );
-
This reply was modified 6 years, 4 months ago by
Franchi Web Design.
-
This reply was modified 6 years, 4 months ago by
Franchi Web Design.
Hello,
Try the code.I think its working fine.
<div class="container"> <div class="row"> <?php $myposts = get_posts( array( 'posts_per_page' => -1, 'post_type' => 'post' ) ); if ( $myposts ) { foreach ( $myposts as $post ) : setup_postdata( $post ); ?> <div class="col-lg-4"> <?php $meta_value = get_post_meta( $post->ID, 'meta-select', true ); echo '<p>'.$meta_value.'</p>'; ?> </div> <?php endforeach; wp_reset_postdata(); } ?> </div> </div>
-
This reply was modified 6 years, 4 months ago by
Steven Stern (sterndata).
Hello @angelch,
your code does not work.@bcworkz kindly you could post the code of the template that works with the save code?
So I try if it works on my theme too.
Thank you so much-
This reply was modified 6 years, 4 months ago by
Franchi Web Design.
How I saved multiple values on my site isn’t from a user input meta box like you want to do, so the code I have will not work for you. A record for post ID 380 did exist at one point, your earlier code output that value at one point. Either it has subsequently been deleted or there’s a ID mismatch in template code. After seeing the meta box code you use, what post 380 has for data doesn’t matter much.
Thanks for the meta box information. It’s not saving data the way you think it is. The correct way of doing so depends on the behavior you are expecting. Should the saved selections be due to multiple selections (with ctrl-clicks) from one form submit, or should it accumulate single selections from multiple form submits?
Are you expecting the multiple selections all be saved in a single meta data array, or single values in multiple meta data entries? The same key in either case, it’s the structure of the saved values that is different.
Also, echoing out the value returned by get_post_meta() in the case of multiple values will not work either. The correct way depends on how the data is being saved. Once I understand what your expectations are, I can offer more specific advice for what to do.
I want to thank you for the help you are giving me!
For example, the function I want to create serves to set up a different layout with a simple customer selection.
For example, I would like to create a selection with these options:
-3-column portfolio (col-md-4),
-4-column portfolio (col-md-3),
-2-column portfolio (col-md-6)
These selectable options should change the value of all posts in the selected category for that type of portfolio.
I could do it from the customizer but it would be more limited, that is, doing it with the meta box would be more intuitive and above all the user could to each new page created for the portfolio select a different layout.
I can not understand why this code not function. The code I created put out of the loop works perfectly, however I will try again, I have a hard head !! ??
Sorry for my bad english!-
This reply was modified 6 years, 4 months ago by
Franchi Web Design.
-
This reply was modified 6 years, 4 months ago by
Franchi Web Design.
Oh! So the multiple values we are looking for are for multiple pages, all with the same single meta value? That is not what I was thinking at all! Sorry for the confusion. And it’s not due to bad English, your English is great.
So you wish to apply this meta value to all posts in a category. That we can do. But then you want to be able to override this selection for individual pages. How would code know whether to apply a selection to the one page and not all pages in the category? If a page has multiple category terms, how does code know which category of pages to apply the selection to? How can someone change the default selection for pages in a category without changing the overrides for specific pages?
I think there are solutions for these questions, but it doesn’t seem to me that this scheme has been thoroughly thought out. Applying such a scheme to multiple pages is more complicated than the logic needed for a single page. There are likely other situations I haven’t brought up that also need to be thought through. Maybe you should work out with pencil and paper all the possible different situations along with what code logic is required in each case so that the result is always as expected. Maybe then the way forward will be clear.
I think you will need both category term meta values combined with page meta values in some cases in order to properly handle the different situations, but I’ve not fully thought this through either.
-
This reply was modified 6 years, 4 months ago by
- The topic ‘WordPress get_post_meta not work in loop’ is closed to new replies.