When i upload the image from wp_media, this below code runs:
add_action('add_attachment', 'auto_post_after_image_upload'); // Wordpress Hook
function auto_post_after_image_upload($attachId)
{
$attachment = get_post($attachId);
$image = wp_get_attachment_image_src( $attachId, 'large');
$image_tag = '<p><img src="'.$image[0].'" /></p>';
$postData = array(
'post_title' => $attachment->post_title,
'post_type' => 'post',
'post_content' => $image_tag . $attachment->post_title,
'post_category' => array('0'),
'post_status' => 'publish',
'post_date' => get_the_time()
);
$post_id = wp_insert_post($postData);
// attach media to post
wp_update_post(array(
'ID' => $attachId,
'post_parent' => $post_id,
));
set_post_thumbnail($post_id, $attachId);
return $attachId;
}
Now i want to put the date of the image when it was created and not the today's date. How can i do that? get_the_time() returns the today date. I want the selected image created date.
You can use this php function exif_read_data
Related
I am adding an image from media in every single post that are created.
To get the attached image and url of that post, I am implementing this code:
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => 5,
'post_status' => null,
'post_parent' => 'any', // any parent
);
$attachments = get_posts($args);
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
setup_postdata($attachment);
$v =$attachment->ID;
$imageurl = wp_get_attachment_url($v);
$postlink = get_permalink($v);
}
The above code works fine for retrieving image url. My question is how do I pass post ID in get_permalink() to make sure that I get link of that post. I know that passing $v in get_permalink() is wrong.
There is a code spinet to print attachment posts.
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'posts_per_page' => 5,
'post_status' => 'inherit',
'post_parent' => 'any', // any parent
);
$loop = new WP_Query($args);
while ( $loop->have_posts() ) : $loop->the_post();
global $post;
echo '<pre>';
print_r($post);
echo '</pre>';
endwhile; wp_reset_query();
Its depending which ID you are looking for inside the loop
$post-ID : That return the post ID for attachment post.
$post->post_parent : It returns the current attachment post parent post ID.
I've a post with post_format = video, in which the content are a video and a text (in the default editor). I need to display the video and the text separately. How can do it?
For now, I've this code:
global $post;
$tmp_post = $post;
$args = array(
'posts_per_page' => 1,
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-video' ) // post format
)
)
);
$video_posts = get_posts( $args );
foreach ($video_posts as $post ) :
setup_postdata( $post );
echo get_the_title($post->ID);
// ------> Here what code for get the video?
// html
// ...
// ...
// ------> Here what code for get the text (if possible)?
endforeach;
$post = $tmp_post;
At the end I found a solution myself: I made a function that return the iframe for the first video inside the post with id $post_id:
function get_first_video_embed($post_id) {
$content = apply_filters('the_content', get_post_field('post_content', $post_id));
$iframes = get_media_embedded_in_content( $content, 'iframe' );
return $video_post_iframe = $iframes[0];
}
I'm using below code to get all the attachments assign to my custom post type..but now i want to get the post id within the loop..how can i do that??
$query = new WP_Query(
array(
'post_type' => 'portfolio', // adjust your custom post type name here
'posts_per_page' => -1,
'fields' => 'ids'
)
);
$image_query = new WP_Query(
array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_mime_type' => 'image',
'posts_per_page' => -1,
'post_parent__in' => $query->posts,
'order' => 'DESC'
)
);
if( $image_query->have_posts() ){
while( $image_query->have_posts() ) {
$image_query->the_post();
$imgurl = wp_get_attachment_url( get_the_ID() );
if(!empty($output)) $output = 'Sorry, no attachments found.';
$output .= '<img src="'.$imgurl.'">';
echo $output;
}
Thanks!
Depending on which ID you are looking for inside the loop, you can do the following
$post-ID -> Returns the post ID from the attachment post being displayed
$post->post_parent -> Returns the current attachment post parent post ID
I have some code to create posts from uploading images into the media library, which is working fine:
add_action('add_attachment', 'create_post');
function create_post( $attach_ID ) {
$attachment = get_post( $attach_ID );
$my_post_data = array(
'post_title' => $attachment->post_title,
'post_type' => 'post',
'post_category' => array('0'),
'post_status' => 'publish'
);
$post_id = wp_insert_post( $my_post_data );
// attach media to post
wp_update_post( array(
'ID' => $attach_ID,
'post_parent' => $post_id,
) );
set_post_thumbnail( $post_id, $attach_ID );
return $attach_ID;
}
The catch is that I don't want it to create new posts from an image upload if I'm manually creating a post. In other words, I want the code to create new posts only if I upload images directly into the media library, not when I added images manually into a new post.
Any help is greatly appreciated!
Maybe there is a better way, but you could use the global variable $pagenow.
global $pagenow;
if($pagenow == 'upload.php'){
//your code here
}
I got some help from a developer on Wizpert. He used the below code to make it work
add_action('add_attachment', 'create_post');
function create_post( $attach_ID ) {
$attachment = get_post( $attach_ID );
if(!$attachment->post_parent)
{
$my_post_data = array(
'post_title' => $attachment->post_title,
'post_type' => 'post',
'post_category' => array('0'),
'post_status' => 'publish'
);
$post_id = wp_insert_post( $my_post_data );
// attach media to post
wp_update_post( array(
'ID' => $attach_ID,
'post_parent' => $post_id,
) );
set_post_thumbnail( $post_id, $attach_ID );
return $attach_ID;
}
}
I´m trying to get the post thumbnail and another attached image from the last post of a custom post type (called parceiros-e-links).
What I got is to get all posts with images and show them... but I need to show only the last post and show it with two images (the_post_thumbnail and the wp_get_attachment_image)
Here is my current code:
<?php
$query = new WP_Query( array( 'post_type' => 'parceiros-e-links', 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'DESC' ) ); //the first loop where I filter the posts from custom post type and by date
if( $query->have_posts() ){
while($query->have_posts()){
$query->the_post();
$image_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => 2, 'post_parent' => get_the_ID() ) ); //the second loop where I filter the posts with attachments and limit to two
while( $image_query->have_posts() ) { $image_query->the_post();
//code below prints the two attachments: thumbnail and another one.
if(has_post_thumbnail()){
the_post_thumbnail('home-parceiros');
}
echo wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' );
}
}
}
?>
I already spent many hours searching similar situations and trying to filter this code but I'm without ideas... Because if I limit the query's first posts_per_page to 1, if the most recent post doesn't has an attachment, no images will be printed!
Any clues about how to limit the number of posts with attachments inside a custom post type?
Thanks!
Your mistakes was at the_post_thumbnail('home-parceiros'); and echo wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' ); Read about the correct attributes given for this two functions here:
http://codex.wordpress.org/Function_Reference/the_post_thumbnail
http://codex.wordpress.org/Function_Reference/wp_get_attachment_image
Here is my suggestion:
$query = new WP_Query(
array(
'post_type' => 'parceiros-e-links',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC'
)
);
if($query->have_posts()) :
while($query->have_posts()) : $query->the_post();
if(has_post_thumbnail()) : the_post_thumbnail(); endif;
$args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo wp_get_attachment_image( $attachment->ID, 'full' );
}
}
endwhile;
endif;
Please let me know :)
Example #2 updated:
$query = new WP_Query( array( 'post_type' => 'parceiros-e-links', 'posts_per_page' => 1, 'orderby' => 'date', 'order' => 'DESC' ) ); //the first loop where I filter the posts from custom post type and by date
if( $query->have_posts() ){
while($query->have_posts()){
$query->the_post();
$image_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => 2, 'post_parent' => get_the_ID() ) ); //the second loop where I filter the posts with attachments and limit to two
while( $image_query->have_posts() ) { $image_query->the_post();
//code below prints the two attachments: thumbnail and another one.
if(has_post_thumbnail()){
the_post_thumbnail('home-parceiros');
}
echo wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' );
}
}
}