wordpress has really great functions when it comes to image attachments - but I can not find any documentation on how to get attachments that are not images .
for now, I wrote this function :
function ok99_get_post_file_attachment($mime='application/pdf',$limit=-1) {
global $post;
$attachments = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment','post_mime_type' => $mime, 'order' => 'ASC', 'orderby' => 'menu_order ID', 'posts_per_page'=>$limit) );
if ($attachments) {
echo 'Your Attachments : <br/>';
foreach ($attachments as $att) {
//$attachment = array_shift($attachments); // debug to delete
echo wp_get_attachment_url($att->ID) . '<br/>';
the_attachment_link($attachment->ID, false);}
}
return false;
}
my question is : is this the only way to get attachments that are not images ?
Is there any way to do that without another query ??
I'm using this technique for querying attachments that are not images or video files, i.e. a "negative" mime type query. It returns an array of post objects (similar to what would be returned by get_posts(), and could easily be modified to exclude other types of attachment media, or made completely generic using vsprintf().
function wpse9927425_get_downloads($post_id) {
global $wpdb;
$sql_query = $wpdb->prepare(
"SELECT * FROM $wpdb->posts
WHERE post_type = %s
AND post_parent = %d
AND post_mime_type NOT LIKE %s
AND post_mime_type NOT LIKE %s
ORDER BY menu_order, post_title ASC",
'attachment',
$post_id,
'image/%',
'video/%'
);
$files = $wpdb->get_results( $sql_query );
return $files;
}
It seems that Once again I will need to answer myself - also because I got no other input for this question
there appears to be no other way of doing that (meaning - there are a lot of ways - but not without another direct query)
I am getting non-image attachments by specifying the MIME type like so:
if ( $attachments = get_children( array(
'post_type' => 'attachment',
'post_mime_type' => array('application/doc','application/pdf', 'text/plain'),
'numberposts' => 15,
)));
foreach ($attachments as $attachment) {
echo 'Download Fact Sheet';
echo '</div>';
}
Related
I am trying to make a shortcode that should pull up the most recent comment (only one), Comment author name and thumbnail of the blog post on which this comment is published no matter if the blog post is the most recent or the older one. The comment should be the most recent one.
I made the code but it is not pulling the most recent comment.
function ct_comment_block(){
$query = new WP_Query(
array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'tax_query' => array(
'taxonomy' => 'calendar_category',
'field' => 'slug'
),
'order' => 'ASC',
'orderby' => 'menu_order'
)
);
$str = '';
$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);`$thumb_url = $thumb_url_array[0];
while ($query->have_posts()):
$query->the_post();
$str .= '<div class="comment-main">
<div class="comment-image-wrapper">'.get_the_post_thumbnail().'</div>
<div class="comment-wrapper">
<h3>'.comment_sender().'</h3>
<h5>Traveler Hobbyist</h5>
<p>'.real_body().'</p>
</div>';
$str .= '</div>';
endwhile;
return $str;
}
add_shortcode('show_comment' , 'ct_comment_block');
I have written most of the my anser in the code comments.
But I would use a $wpdb query to fetch the newest comment, get the id of the assosiated post and work from there.
Hopefully this makes sense to you.
<?php
add_shortcode('show_comment' , 'ct_comment_block');
function ct_comment_block(){
if(!is_admin()){
global $wpdb;
$query = "SELECT * from $wpdb->comments WHERE comment_approved= '1' ORDER BY comment_date DESC LIMIT 0 ,1"; // This shows the newest comment
$comments = $wpdb->get_results($query);
$strToReturn = '';
if ($comments) {
$strToReturn .= '<div class="someSortOfWrappingClass">';
foreach ($comments as $comment) {
//Use $comment->comment_post_ID for the post_id like so
$link = get_permalink($comment->comment_post_ID);
//but you can check what else is inside $comment - like $comment->comment_author_email etc.
//Simply gather your html here
} //End of foreach
$strToReturn .= '</div>';
} //End of IF
return $strToReturn;
} else {
return;
}
}
Try it may help you for get most recent comments
$recentcomments = get_comments( array(
'number' => 10, // fetch number of comments.
'status' => 'approve', // status approved comments.
'post_status' => 'publish' // post status published comments.
) );
if ( $recentcomments ) {
foreach ( (array) $recentcomments as $comment ) {
echo '' . get_the_title( $comment->comment_post_ID ) . '';
}
}
I am creating wp_insert_post to create post from json, while creating post i need to create meta values i have tried both add_post_meta and update_post_meta no results Can anybody help
foreach ( $response->data as $single_data ) {
$post_title = $single_data->name;
$video_id = $single_data->uri;
$thumnbnail_url = $single_data->pictures->sizes[4]->link;
if (!post_exists($post_title)) { // Determine if a post exists based on title, content, and date
$post_id = wp_insert_post(array(
'post_type' => 'vimeo_videos',
'post_title' => $post_title,
'post_status' => 'publish',
));
}
$newPostID = wp_insert_post($post_id);
global $post;
add_post_meta( $post->ID, 'vimeo_video_thumnbnail_url_key', $thumnbnail_url, true );
update_post_meta( $newPostID, 'video_url_id', $video_id );
You're using wp_insert_post twice which does not make sense. The first wp_insert_post is correct and will return the $post_id of the created post upon success. However, your
$newPostID = wp_insert_post($post_id);
is totally wrong and will always store 0 as a result in $newPostID, regardless of whether the post existed beforehand or not since $post_id will never contain a valid post array. What you want is to get the ID of the existing post (which post_exists already returns if successful). Change your code like so:
$existingPostID = post_exists($post_title);
if (!$existingPostID) {
$existingPostID = wp_insert_post(array(
'post_type' => 'vimeo_videos',
'post_title' => $post_title,
'post_status' => 'publish',
));
}
if ($existingPostID) {
update_post_meta( $existingPostID, 'video_url_id', $video_id );
}
For insert new post used this syntax:
add_post_meta( int $post_id, string $meta_key, mixed $meta_value, bool $unique = false )
I have a "taxonomy" custom field for the user pages. I want to build a query filtered by this field. It works with normal querys but not with user-querys, am i doing something wrong?
<?php
$args = array(
'key' => 'fruits',
'value' => 'apple'
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo $user->display_name;
}
} else {
echo 'No users found.';
}
?>
Try get_users instead:
$users = get_users(array(
'meta_key' => 'fruits',
'meta_value' => 'apple'
));
var_export($users);
Wordpress codex: get_users()
Edit:
After a bit of research it turns out that get_users() is only a wrapper for WP_user_query, so switching to this function will make no difference.
However... did you notice that in my answer (and vrajesh') we have substituted your key with meta_key, and value with meta_value ... They are definitely defined in the WP_User_Query class, so I would be surprised if they didn't have any meaning.
If by chance you are using your original $args (which I guess does not actually refer to fruits and apples), then that may well be the explanation you are getting nothing.
try this:
$args = array( 'meta_key' => 'fruits', 'meta_value' => 'apple','compare' => '=');
Use WP_Query instead of WP_User_Query. WP_User_Query is used to retrieve data from user and usermeta table. And according to my understanding your are retrieving data from posts and postmeta table.
Class Reference/WP User Query and
Class Reference/WP Query
UPDATED:
Try this
<?php
$args = array(
'meta_query' => array(
'key' => 'fruits',
'value' => 'apple',
'compare' => '='
)
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo $user->display_name;
}
} else {
echo 'No users found.';
}
?>
Hi I´m trying to know if there are someway to remove the relation of the uploaded files with a post WITHOUT deleted the files, when you upload a file and inserted into the post. For example a pdf file. you will get a link inserted into the post. what I want to do is if I remove this link from the post. Delete the reference of this file from the post in the database.
The problem I'm facing is that I'm using a function wich return all the pdf files uploaded into a custom type of post.
function getPdfList(){
global $post, $posts;
$list = array();
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
$ext = pathinfo($attachment->guid, PATHINFO_EXTENSION);
if("pdf" == strtolower($ext)){
$list[] = $attachment;
}
}
}
return $list;
}
So then I'm doing this in my php file
$args = array( 'post_type' => 'fuerzabasica', 'posts_per_page' => 40 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$pdfs = getPdfList();
echo '<div class="entry-content" style="">';
//the_content();
foreach ($pdfs as $pdf) {
echo $pdf->guid."<br />";
}
echo '</div>';
endwhile;
?>
The problem is that I'm still getting files wich I remove the link from the post, so if my USER upload new files he will get the old files(which the link was removed) and the new one. Is a way to delete the reference of this files of my post??
WordPress will maintain a parent/child relationship between posts and uploaded files. To prevent this you will have to make sure the post_parent value of every attachment is 0 instead of the parent posts ID.
You could accomplish this with a save_post filter, in which you'll loop through any attachments returned by get_posts(array('post_type' => 'attachment', 'post_parent' => $post_id, 'posts_per_page' => -1)); setting post_parent to 0. $post_id is the first parameter of your filter function.
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.'" />';
}
}