How can I get the Posts created with ACF in Wordpress? - wordpress

I added this to my function.php for saving the post:
function my_pre_save_post( $post_id )
{
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id;
}
// Create a new post
$post = array(
'post_status' => 'draft' ,
'post_title' => 'A title, maybe a $_POST variable' ,
'post_type' => 'post' ,
);
// insert the post
$post_id = wp_insert_post( $post );
// update $_POST['return']
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
And on the page.php I've got this code:
$postid = get_the_ID();
if($postid ==50){ //50 is a Page I created for the Form
$options = array(
'post_id' => 'new',//$post->ID, // post id to get field groups from and save data to
'field_groups' => array(46), // 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' => '',
'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( $options );
}
Now, I think the saving was successfull, how do I get the whole Posts that have been created with this Form?
I tried this (on page with ID 51), but I get nothing:
$posts = get_posts(array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'location',
'meta_value' => 'melbourne'
));
if($posts)
{
foreach($posts as $post)
{
the_field('titel');
}
}
The ACF Plugin for Wordpress is very well documented, but I couldnt solve my problem.
http://www.advancedcustomfields.com/resources

according to the ACF form field documentation https://www.advancedcustomfields.com/resources/acf_form/
the "return" field has 2 dynamic placeholders
"(String) The URL to be redirected to after the form is submitted.
Defaults to the current URL with a GET parameter ‘?updated=true’. A
special placeholder ‘%post_url%’ will be converted to post’s
permalink. A special placeholder ‘%post_id%’ will be converted to
post’s ID."
if you add 'return' => '?newpost=%post_id%', for example, you get back your original page with the new post id as URL parameter that you can handle with PHP in your page template

The documentation says you've to set the post_id value inside your $options array to new_post instead of new if you want to create a new post. Also you've to use the new_post array key with an array with data for the new post as value for the key.
Check out the acf_form documentation. This is what I basically mean:
$options = array(
'post_id' = 'new_post',
'new_post' = array(
//new post data
)
);

Related

Automatic deletion of published WordPress posts

I have created a custom post similar to WordPress posts that I want to delete automatically every time (for example, every day or every week).
Is there a function for this?
I know that you can delete trash posts with the following function
define('EMPTY_TRASH_DAYS', 10 );
But what about custom created posts?
Thanks for your help
I think, this code will full fill your requirements.
// Define the custom post type that you want to delete automatically
$custom_post_type = 'your_custom_post_type';
// Function to delete the custom post type posts
function delete_custom_post_type_posts() {
// Get all published posts of the custom post type
$current_date = current_time('Y-m-d');
$yesterday_date = date('Y-m-d', strtotime('-1 day', strtotime($current_date)));
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'date_query' => array(
'after' => $yesterday_date,
'before' => $current_date,
'inclusive' => true
)
);
$my_posts = get_posts( $args );
// Loop through the posts and delete them
foreach ( $my_posts as $my_post ) {
wp_delete_post( $my_post->ID, true );
}
}
// Schedule the event to run every week
if ( ! wp_next_scheduled( 'delete_custom_post_type_posts_event' ) ) {
wp_schedule_event( time(), 'twicedaily', 'delete_custom_post_type_posts_event' );
}
// Hook the function to the scheduled event
add_action( 'delete_custom_post_type_posts_event', 'delete_custom_post_type_posts' );```
You can delete published posts every week by setting up a wp_schedule_event
Hope this code helps
// Define the custom post type that you want to delete automatically
$custom_post_type = 'your_custom_post_type';
// Function to delete the custom post type posts
function delete_custom_post_type_posts() {
// Get all published posts of the custom post type
$current_date = current_time('Y-m-d');
$last_week_date = date('Y-m-d', strtotime('-1 week', strtotime($current_date)));
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'date_query' => array(
'after' => $last_week_date,
'before' => $current_date,
'inclusive' => true
)
);
$posts = get_posts( $args );
// Loop through the posts and delete them
foreach ( $posts as $post ) {
wp_delete_post( $post->ID, true );
}
}
// Schedule the event to run every week
if ( ! wp_next_scheduled( 'delete_custom_post_type_posts_event' ) ) {
wp_schedule_event( time(), 'weekly', 'delete_custom_post_type_posts_event' );
}
// Hook the function to the scheduled event
add_action( 'delete_custom_post_type_posts_event', 'delete_custom_post_type_posts' );

Elementor custom query with two WP_Query requests

I'm using WordPress and Elementor. There are 2 post types named "albums" and "clips". The clips post type stores video clips for specific album under albums post type. A custom Elementor query displays all albums under which user has uploaded the clips.
When the queries are tested on a testing page, it works fine. But when included under Elementor custom query, it doesn't work. Here is the code,
function user_contributed( $query ) {
// initialize array
$albums[] = '';
// Get logged in user id
$user_id = get_current_user_id();
if(isset($user_id) && !empty($user_id)){
$user_info = get_userdata($user_id);
// Get user email ID
$user_email = $user_info->user_email;
// Get the user contributed clips from "clips" post type
$args = array( 'post_type' => 'clips', 'posts_per_page' => -1,
'meta_query' => array( array( 'key' => 'user_email', 'value' => $user_email, 'compare' => '=' ), 'relation' => 'AND', array( 'key' => 'clip_status', 'value' => 'ready', 'compare' => '=' ) ),
);
$contributed_query = new WP_Query( $args );
if($contributed_query->have_posts()) {
if($contributed_query->post_count > 0){
while($contributed_query->have_posts() ) {
$contributed_query->the_post(); $postid = get_the_ID();
// Get album ID from ACF field
$albumid = get_field( "album_id", $postid );
if(!in_array($albumid, $albums)){ $albums[] = $albumid; }
}
}
}
// Get all albums from "album" post type, where logged-in user has contributed
$query->set( 'post_type', [ 'albums' ] );
$query->set( 'post__in', $albums );
}
}
add_action( 'elementor/query/user_contributed', 'user_contributed' );
I think the first query is overwriting or conflicting with the main query. I tried wp_reset_postdata, wp_reset_query and pre_get_posts but didn't work. The Elementor custom query documentation wasn't helpful. Is it possible to execute a query before the main query?
I tried wp_reset_postdata, wp_reset_query and pre_get_posts but didn't work.

i want to get woocommerce thumbnail src url in array

I want get thumbnail image url in the following image attribute
$product = wc_get_product( $post->ID ); //do this instead
$end_point = 'https://api.webpushr.com/v1/notification/send/sid';
//do a wp_remote_post instead of cURL
$body = array(
'title' => $product->get_name(),
'message' => 'check out now',
'target_url' => $product->get_permalink(),
'image' => $product->get_image($attr = array( 'src'=>get_the_post_thumbnail_url())), // Here I want to get Image thumbnail url. But it is not working
'sid' => '113858026',
);
You can write it like this:
'image' => wp_get_attachment_image_url( $product->get_image_id(), 'thumbnail' )
where you get the image id using $product->get_image_id() then you use wp_get_attachment_image_url to get the url.

Fieldmanager Wordpress plugin

I need to find a way to display values saved in custom meta boxes. Meta Boxes are created with WP Fieldmanager plugin.
Here is the code used to display fields in the post admin:
add_action( 'init', function() {
$fm = new Fieldmanager_Group( array(
'name' => 'sidebar',
'limit' => 0,
'label' => 'Sidebar Section',
'label_macro' => array( 'Section: %s', 'name' ),
'add_more_label' => 'Add another Sidebar Section',
'children' => array(
'name' => new Fieldmanager_Textfield( 'Section Heading' ),
'item' => new Fieldmanager_Textfield( 'Item', array(
'limit' => 0,
'one_label_per_item' => true,
'add_more_label' => 'Add another Item',
) ),
),
)
);
$fm->add_meta_box( 'Sidebar', array( 'post' ) );
} );
You can see this is actually a repeating group which has a repeating field inside. I need to display meta values from those groups.
Thanks.
You have to use foreach() to get out the content:
$repeating_fields = get_post_meta( get_the_ID(), 'sidebar', true); //name of your custom field;
foreach ($repeating_fields as $repeating_field) {
print_r($repeating_field[name]); //use here the name given to your children in array to select the correct one;
}
From here you if know how to arrange a foreach loop you should be good.
If you want to get the metabox content as an array you can do like this:
$repeating_fields = get_post_meta( get_the_ID(), 'sidebar', true);
$name = array(); //define the variable as an array from the start;
foreach ($repeating_fields as $repeating_field) { //start the loop
$name[] = $repeating_field[name]; //get the array content from the meta box
}
echo $name; //echo the array or parts of it

Can personalized custom (database) data be added to a post using the wordpress API

Ok, so how do I put this simply and sweetly. It's a wordpress issue
$post = array(
'post_type' => $type,
'post_title' => $title,
'post_content' => $content,
'post_status' => $status,
'post_name' => $slug
);
wp_insert_post($post);
The code above creates a post, but I'd like my post to be able to hold custom key => values of my own choosing instead of simple html "content".
I'm pulling several values of information (title, category, summary, thumbnail_url) from an external RSS feed. At the moment, I am just formatting it into html and adding it all to a page as "content", whereas I'd prefer to be able to store this information as it is, so that each individual value can be accessed relative to the 'post' it's attached to and modified later on, on a whim.
i.e $page = get_page_by_title('title');
$summary = $page('summary);
I want to create several "posts" with their own custom values (title, category, summary, thumb, url).
I also want to display all of these "posts" on one 'page', but that's a question for another time.
Can it be done?
You can use the add_post_meta function along with the return value of wp_insert_post to insert the custom data as custom fields.
$post = array(
'post_type' => $type,
'post_title' => $title,
'post_content' => $content,
'post_status' => $status,
'post_name' => $slug
);
$post_id = wp_insert_post($post);
add_post_meta($post_id, "title", "Behind Foo Bars");
Then you can retrieve it with get_post_meta
get_post_meta($post_id, "title");

Resources