After adding a post, no data is output acf - wordpress

After the program has been programmed to add a post through the function
wp_insert_post()
Can not get data and fields in acf plugin.
The data is available inside the post, in the fields, but not on the client side.
The output is obtained only after clicking the "Update" button inside the administrative part. After that, the data is output
Filling the post is as follows:
$post_data = array(
'post_title' => wp_strip_all_tags($item->name),
'post_content' => "",
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_category' => array(3,13,2)
);
$post_id = wp_insert_post( $post_data );
update_field( "link_to_1", $item->image, $post_id );
update_field( "link_to_2", $item->tour, $post_id );
The conclusion on the client side is as follows:
<img src="<?php the_field('link_to_1');?>"/>

Assuming that link_to_1 is storing image url .
In acf field you have given the return value as image url.
<img src="<?php the_field('link_to_1');?>"/>

Related

Wordpress wp_insert_post in post type doesnt work

I created a custom post type with CPT plugin. It's works fine and did the job.
I can create post, publish the post and i can see on the site.
But when i create a post from frontend via wp_insert_post it's creates the post but i can't see on site. It gives me 404.
$postId = "" . getUserCompanyId() . $menuId . "";
$m = array(
'insert_id' => intval($postId),
'post_title' => $menuName . ' | ' . $postId,
'post_name' => ''.$postId.'',
'post_content' => '',
'post_status' => 'draft',
'post_author' => get_current_user_id(),
'post_type' => 'm',
);
$insert = wp_insert_post( $m );
When i create a post:
When i publish and see on site:
Note: wp_insert_post works just fine when change post_type to post
insert_id is not allowed in post arguments array($m in your code) it must be ID.
But as you mentioned you need to create a post then ID is not required it is only needed when you wish to update an existing post.
Refer here for wp_insert_post(...)
Also, validate whether the post is created on not
$post_args = array('post_type' => 'your_custom_post',
/*other default parameters you want to set*/
);
$post_id = wp_insert_post($post_args);
if(!is_wp_error($post_id)){
//the post is valid
} else {
//there was an error in the post insertion,
echo $post_id->get_error_message();
}

Advanced Custom Field Front End Form not producing the right Slug/Permalink

I've grabbed this code from the Advanced Custom Fields website to make an employee directory with a front-end ACF form. But I can't get it to produce a permalink that is the title employee name, which is the title of the post. It just produces a random number. Permalinks are set to Post Name Here is the code in my template file:
<?php while ( have_posts() ) : the_post(); ?>
<?php
acf_form(array(
'post_id' => 'new_post',
'post_title' => wp_strip_all_tags($_POST['acf']['field_624c65c9cc5da']), // Post Title ACF field key
'return' => '%post_url%',
'new_post' => array(
'post_type' => 'staff',
'post_status' => 'publish'
)
));
?>
<?php endwhile; ?>
And this is the code in my functions PHP file:
add_action('acf/save_post', 'my_save_post', 20);
function my_save_post($post_id){
// Get the data from a field
$new_title = get_field('staff_name', $post_id);
// Set the post data
$new_post = array(
'ID' => $post_id,
'post_title' => $new_title,
);
// Remove the hook to avoid infinite loop. Please make sure that it has
// the same priority (20)
remove_action('acf/save_post', 'my_save_post', 20);
// Update the post
wp_update_post( $new_post );
// Add the hook back
add_action('acf/save_post', 'my_save_post', 20);
}

Creating a page with wp_insert_post()

I have a form that collects data about a user's product and then creates a page in WordPress with
$my_post = array(
'post_content' => "My page content",
'post_title' => $product_title,
'post_name' => $product_title,
'post_type' => 'page', // must be 'page' to accept the 'page_template' below
'page_template' => "listing.php",
'post_status' => "publish"
);
$ID = wp_insert_post( $my_post );
$permalink = get_permalink($ID);
echo "<br />ID for new page is $ID, Permalink for new page is $permalink";
The form data is put into meta variables for the page ID and the listing.php template file pulls it out of there and builds the HTML to display the product page. This all works fine and I can see that the page meta variable, _wp_page_template, gets set correctly to the template file I specified, listing.php:
Now I want to create a second page from the same form data, this one displaying different parts of the data in a different way. So I've added a second block of code, starting at $my_cert below, that creates this second page and specifies a different template, certificate.php, that knows how to build the second version of the data.
$my_post = array(
'post_content' => "My page content",
'post_title' => $product_title,
'post_name' => $product_title,
'post_type' => 'page', // must be 'page' to accept the 'page_template' below
'page_template' => "listing.php",
'post_status' => "publish"
);
$ID = wp_insert_post( $my_post );
$permalink = get_permalink($ID);
echo "<br />ID for new page is $ID, Permalink for new page is $permalink";
$my_cert = array(
'post_content' => "My certificate", // post_content is required
'post_title' => "My certificate", // post_title is required
'post_name' => "My certificate",
'post_type' => 'page', // must be 'page' to accept the 'page_template' below
'page_template' => "certificate.php",
'post_status' => "publish"
);
$CERT_ID = wp_insert_post( $my_cert );
$cert_permalink = get_permalink($CERT_ID);
echo "<br />ID for new certificate is $CERT_ID, Permalink for new certificate is $cert_permalink";
But when I look in the meta data for the second page created, the template is set to "default" instead of certificate.php:
I know I've set up certificate.php correctly as a template (set /* Template Name: certificate */ at the top) because the Page Edit Template dropdown includes certificate:
So does anyone see why I can't create this second page with the template set to certificate.php?
Thanks
Are you sure your page template src for: certificate.php is: certificate.php? And not: templates/certificate.php or something like that. Look in your theme folder and be 100% of the page template path. Check your spelling or for typos in the page template path or name. It must be an exact match.
If you still have problems I would look into and debug the source code of: wp_insert_post()
if ( ! empty( $postarr['page_template'] ) && 'page' == $data['post_type'] ) {
$post->page_template = $postarr['page_template'];
$page_templates = wp_get_theme()->get_page_templates( $post );
if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) {
if ( $wp_error ) {
return new WP_Error('invalid_page_template', __('The page template is invalid.'));
}
update_post_meta( $post_ID, '_wp_page_template', 'default' );
} else {
update_post_meta( $post_ID, '_wp_page_template', $postarr['page_template'] );
}
}
So its probably this part that fails:
if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) )
Try to modify: wp-includes/post.php and go to the definition of: function wp_insert_post() on row: 2872. And add a new row on row: 3312 for debugging purposes.
echo '<pre>';
print_r( $page_templates );
echo '</pre>';
die();
Make sure your certificate.php is among those in that array. Remember to delete the debug code before continuing. This should give you some answers.

Update wordpress post from front-end (using acf)

I am making front end admin for users to add/edit their posts. I already made post add form and it works, but edit form doesnt work.
functions.php
function add_new_post( $post_id )
{
if( $post_id == 'new' ) {
// Create a new post
$post = array(
'post_title' => $_POST["fields"]['field_52c810cb44c7a'],
'post_category' => array(4),
'post_status' => 'draft',
'post_type' => 'post'
);
// insert the post
$post_id = wp_insert_post( $post );
return $post_id;
}
else {
return $post_id;
}
}add_filter('acf/pre_save_post' , 'add_new_post' );
index.php
<div id="updateform-<?php the_ID(); ?>" class="collapse">
<?php
echo get_the_ID();
$args = array(
'post_id' => get_the_ID(), // post id to get field groups from and save data to
'field_groups' => array(31), // this will find the field groups for this post (post ID's of the acf post objects)
'form' => true, // set this to false to prevent the <form> tag from being created
'form_attributes' => array( // attributes will be added to the form element
'id' => 'post',
'class' => '',
'action' => get_permalink( get_the_ID() ),
'method' => 'post',
),
'return' => add_query_arg( 'updated', 'true', get_permalink() ), // return url
'html_before_fields' => '', // html inside form before fields
'html_after_fields' => '', // html inside form after fields
'submit_value' => 'Update', // value for submit field
'updated_message' => 'Post updated.', // default updated message. Can be false to show no message
);
acf_form( $args );
?>
</div>
For saving posts you should use save_post not pre_save_post.
Basically pre_save_post is used for new posts.
Use this below add_filter('save_post' , 'add_new_post');
I created plugins for that:
Forms actions
https://wordpress.org/plugins/forms-actions/
Add actions to yours ACF forms.
ACF Frontend display
https://wordpress.org/plugins/acf-frontend-display/
Display your ACF form on frontend
If by some chance you happen to be using Elementor Page Builder, you can do this by installing Advanced Widgets for Elementor. It comes with an ACF Form widget that you can just drag & drop anywhere into your site and configure your form through the UI (no coding required).

How do i show wordpress attachments from current post?

So with my blog i have a photo attachment page but it only shows to photo's at a time, and those two photo's are used as the navigation and i hate that.
I want the attachment page to show all the photo's that goes along with the rest of that set.
Here is the current code
<div id="nav-images" class="navigation clearfix">
<div class="nav-next"><?php next_image_link() ?></div>
<div class="nav-previous"><?php previous_image_link() ?></div>
How do i change that to show all the post attachments?
To clarify, this doesn't work anymore - at least with version 3.5.2. I used this instead;
$attachments = get_children(
array(
'post_type' => 'attachment',
'post_parent' => get_the_ID()
)
);
foreach ($attachments as $attachment) {
// ...
}
Only resurrecting an old thread because this one ranks quite highly for this search term.
When you're on a page or post, you can get all of its attachments with the following:
global $post; // refers to the post or parent being displayed
$attachements = query_posts(
array(
'post_type' => 'attachment', // only get "attachment" type posts
'post_parent' => $post->ID, // only get attachments for current post/page
'posts_per_page' => -1 // get all attachments
)
);
foreach($attachements as $attachment){
// Do something exceedingly fancy
}
Since you're currently on an attachment page, you can get all the other attachments using the $post->post_parent value:
global $post; // refers to the attachement object
$attachements = query_posts(
array (
'post_type' => 'attachment', // only get "attachment" type posts
'post_parent' => $post->post_parent, // attachments on the same page or post
'posts_per_page' => -1 // get all attachments
)
);
To then display the attachment images, you can use the wp_get_attachment_image_src function. The attachment's ID will be available in each iteration of your foreach loop as $attachement->ID (if you use the same naming convention as my first example).
Since WordPress 3.6.0 you can also use get_attached_media.
$media = get_attached_media( 'image', $post->ID );
if(! empty($media)){
foreach($media as $media_id => $media_file){
$thumbnail = wp_get_attachment_image_src ( $media_id, 'thumbnail' );
$full = wp_get_attachment_url( $media_id );
echo '<img src="'.$thumbnail[0].'" alt="'.$media_file->post_title.'" />';
}
}

Resources