Support Forum
Code question: I have a field Post with related posts. The code I use for display works. When there is no related post then it displays a empty value. I think the problem is in the IF statement.
Can anybody help me?
<?php
global $post;
$field_id = "hoogtepunten";
$post_id = rwmb_meta( $field_id );
if ( $post_id ) {
echo '<h2 class="related-content-title">Bezienswaardigheden langs de route</h2>';
echo '<div class="related-content">';
foreach( $post_id as $post) {
setup_postdata( $post ); ?>
.... some data ....here it goes wrong
<?php }
echo '</div>';
wp_reset_postdata();
}
?>
Hi Cees,
There is no problem with the if
statement in your code. Nothing displays if the condition is not true, it's normal behavior. You can add else
statement to display something if the condition wrong.
if ( $post_id ) {
echo "True";
} else {
echo "False";
}
Get more details here https://www.w3schools.com/php/php_if_else.asp
Hi,
It doesn't change anything.
if ( $post_id ) {
echo "True";
} else {
echo "False";
}
gives a true on every page (with or without related posts). I have migrate from ACF to METABOX with the tool. I don't understand what is happen.
It is a bug. I have do the new migration tool.
This code $arrayLength = count($post_id) show 1 when there is no related post.
I have edit the and save and got a error-message that I have to fill al the fields (required) but that isn't required.
I go to metabox and save the Custom Fields without changing anything. Now the results on the front-end are oke and I can change the post without fill all the fields.
I see this message on all posts where I use the Custom Fields that are migrate from ACF.
Hi Cees,
Can you please share the debug log and some screenshots of the error message?
Hi Long,
Which debug log do I have to send?
You can see here an example:
https://oxygen.groetenuitleusden.nl/doen/hardlopen-in-asschat/
(removed)
You see that at below the heading Horeca langs de route an empty picture and link. This is what happens (also at heading Bezienswaardigheden langs de route) where are no related post at the current post. When I save this post then the problem is gone.
Hi Cees,
Here is the code that you shared
global $post;
$field_id = "hoogtepunten";
$post_id = rwmb_meta( $field_id );
if ( $post_id ) {
echo '<h2 class="related-content-title">Bezienswaardigheden langs de route</h2>';
echo '<div class="related-content">';
foreach( $post_id as $post) {
setup_postdata( $post ); ?>
.... some data ....here it goes wrong
<?php }
echo '</div>';
wp_reset_postdata();
}
and I see the heading, post related in one code block display as well https://share.getcloudapp.com/lluN7Ogx
What is the code block that you show the heading "Horeca langs de route"?
You also said:
This code $arrayLength = count($post_id) show 1 when there is no related post.
I have edit the and save and got a error-message that I have to fill al the fields (required) but that isn't required.
Can you please share some screenshots of the whole scenario?
Hi Long,
The website is live now. You can see there problem here: https://www.groetenuitleusden.nl/doen/hardlooproute-amersfoort/
I share a video what is happening :
https://api.vadoo.tv/landing_page?vid=63wYAKBAmkbwY0ELr29PducxHNpowzOW
Hi,
Thanks for your additional information.
Can you please export the field group of the ACF or share the generated PHP code when exporting? I will try to import that field group to my site and migrate data to reproduce the issue.
And please share some screenshots/screen record in your database, table wp_postmeta, meta key: field ID before updating the post. It looks like the meta value (field value) is not really empty.
Or change the condition to
if ( !empty ( $post_id ) ) {
...
}