I have no idea how to get the date of attachments. I acquire the attachments as follows:
$attachments = get_children(array('post_parent'=>$post->ID, 'post_type'=>'attachment', 'post_mime_type'=>'image'));
foreach ( $attachments as $attachment ) {
//I want to get the date of the attachment
}
Any ideas? Thanks for looking!
Attachment is a type of post, a Custom Post Type at the end of the day. As so, they share exactly the same table and characteristics as other post types.
It's just a matter of:
foreach ( $attachments as $attachment ) {
echo $attachment->post_title . ' - ' . $attachment->post_date;
}
If you need specific attachments metadata, they have specialized functions, like wp_get_attachment_metadata as suggested by #brbcoding in comments.
Related
I'm trying to save Query Vars to a post for later retrieval.
I'm using permalinks in this format: domain.com/%category%/%postname%/
Example:
I create a following page
domain.com/page-003/
I add Query Var called email to the page
add_query_arg('email', 'test#abc.com', 'domain.com/page-003/')
Now when I call
get_permalink($post_id);
I get
domain.com/page-003/
Instead of
domain.com/page-003/?email=test#abc.com
What am I missing? Aren't Query Vars saved with a post?
You want to save some meta data which you want to restore later on and add as query arg into the URL.
You need to first save it as post_meta like e.g. when you save post with that data. You use:
<?php update_post_meta(get_the_ID(), 'email_address', 'abc#mail.com'); ?>
More details: https://codex.wordpress.org/Function_Reference/update_post_meta
Then during the retrieval, you may hook into a HOOK early on like template_redirect or earlier of the post you can get post_meta to get the email and then add to query arg:
<?php $email = get_post_meta( get_the_ID(), 'email_address' ); ?>
Then
esc_url( add_query_arg( 'email', $email, get_permalink( get_the_ID() ) ) );
Something like that, code is untested, I just wrote here, you may please read detailed doc in codex for each function used above.
Update: How to update/fill Ninja form field from Meta Value:
add_filter( 'ninja_forms_render_default_value', 'wm_the_value' , 10 , 3);
function wm_the_value( $default_value, $field_type, $field_settings ) {
if( 'textbox' == $field_type && in_array('ref' , $field_settings)){
$default_value = get_post_meta(get_the_ID(),'_listing_mls', true);
}
return $default_value;
}
'ref' is field name in Ninja form.
'_listing_mls' is meta_key name from WP database.
Hope it works for you.
I have a plugin in mind for Wordpress (that I'm aiming to create) that bascially are going to use the same features of the media - library (uploading / viewing / setting descriptions etc on images).
Is there a way of "copying" the media library and then use it/modifiy it as you wish?
Background:
I want to categories these photos for different customers (that the owner of the site uploads). I believe the built-in media library should be used for site-specific photos (like logos, product photos etc). I don't want mix built-in "media-library" with specific customers photos.
Guidelines
Maybe there's a better approach. (Is it better to create categories in the media-categories for each customer?) If there is, please tell me. I'm looking for guidelines - not a solution.
Solution 1:
First query posts, and then for each one of them use something like this code:
$images = get_children( array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => -1
) );
foreach ( (array) $images as $image ) {
print wp_get_attachment_url( $image->ID );
}
I don't like this solution because it will generate many SQL queries.
Solution 2:
Write your own SQL query which will select all images at once. It should look something like this:
// change this to your custom post type, attachment in your case.
$post_type = 'attachment';
global $wpdb;
$where = get_posts_by_author_sql( $post_type );
$query = "SELECT * FROM $wpdb->posts p where p.post_type = 'attachment' AND (p.post_mime_type LIKE 'image/%') AND (p.post_status = 'inherit') AND p.post_parent IN (SELECT $wpdb->posts.ID FROM $wpdb->posts {$where} ) ORDER BY p.post_date DESC";
$results = $wpdb->get_results( $query );
if ( $results ) {
foreach ( (array) $results as $image ) {
print wp_get_attachment_url( $image->ID );
}
}
The way I would build that plugin, is creating a custom post type, similar to the media upload type, and then modify it as you wish (adding a category for example).
I like the idea of the plugin, so let me you know then you finish, Good luck.
Edit: If you are actually want to copy all the image files to another directory you could do something like this using :
$source = wp_upload_dir(); // get WordPress upload directory
$dest= "wp-content/My-new-Directory"; // where you would like to copy
mkdir($dest, 0755);
foreach (
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST) as $item
) {
if ($item->isDir()) {
mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
} else {
copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
}
}
Theoretically, you could copy the whole media gallery.
Media gallery (in admin panel) is basically a post type archive. Post type in question being attachment.
By defining your custom post type, you could add all the action, display and functionality hooks needed to replicate the attachment functionality.
If you want to categorize your attachment either by using a hierarchical or non hiearchical taxonomy you could register a custom taxonomy for the attachment post type.
Both ways are semi-complex, and are perhaps unneeded. Would you be so kind to describe your usage scenario in more detail, so I could help you better.
In my wordpress page School I have two different blog. let's say blog A and blog B. When use on either blog A or blog B he can make comment. Now I want to make the comments mutual of both blogs.
i.e
If someone comments on the blog A it should display like below:
on the blog A itself
on the School page
and also on the blog B
So no matter where user has comment on the above mentioned three pages comment go on all three pages.
You could use the rss feeds to get comments and load those into the page.
If it is a multisite you could take a look here.
If the sites are separate but on the same server you could make a connection to there databases and query the comments you need.
You will need to code something yourself at the very least.
You can get comments of a post by using WP_Comment_Query. A rough example:
<?php
$args = array(
'post_ID' => 1 //Add the postID of the post you need here
);
// The Query
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
// Comment Loop
if ( $comments ) {
foreach ( $comments as $comment ) {
// here you can display the comment in the way you want
echo 'Author: ' . $comment->comment_author . '<br/>';
echo '<p>' . $comment->comment_content . '</p>';
}
} else {
echo 'No comments found.';
}
?>
Do take a look at the article mentioned above. I don't know how you want to get the post_ID but that's up to you. Let me know if you need help.
I'm using the Types Wordpress plugin to enable custom fields. The plugin allows you to rearrange the order of the images in the admin page editor. Here's my code in my single.php to display multiple images in the custom field and have a link to itself to also use Fancybox:
<?php
$mykey_values_img = get_post_custom_values('wpcf-image');
if ($mykey_values_img != null) {
foreach ( $mykey_values_img as $key => $value ) {
?>
<img src="<?php echo $value; ?>" />
<?php
} //foreach
} //if
?>
Problem:
Right now this code works perfectly on my local copy running on MAMP. However, when I put it online hosted on iPage, the images are out of order. I don't know what's causing this discrepancy. When I use the shortcode from Types to display the images instead of the php above they are in order, but I don't have the option of using Fancybox. I was also wondering if there is a better way to display an image from Wordpress that will insert the alt tag for the image.
I just encountered this problem too, and your first answer led me to a tighter solution.
I also used types_render_field(), but if you include a 'raw' parameter, you can avoid the string manipulation.
$images_raw = types_render_field('image', array('raw'=>'true','separator'=>';'));
$images = explode(';', $images_raw);
foreach ($images as $link) {
echo '' . $link . '">"';
}
Then, if you're nasty, you can get the ID of the attachment from its SRC. Using that ID, you can get all the info you need about that attachment, like a caption, or whatnot.
I figured out a work around to get it working. Its not ideal but it works.
The Types plugin came with its own php function to display the custom field called types_render_field. It displayed my images in order. To get fancybox working I had to do sort of a hack on the string. Here's the code:
$images = ( types_render_field( "image", array( 'size' => 'full', 'alt' => get_the_title(), 'title' => get_the_title() ) ) );
$imgArray = explode(">", $images);
foreach ( $imgArray as $value ) {
$pos1 = strpos($value, 'src="', 0)+5;
$pos2 = strpos($value, '" ', $pos1);
$link = substr($value, $pos1, $pos2 - $pos1);
echo ''.$value.">";
}
I have hard time trying to find the solution, does somebody know how to get this:
I have one WordPress post in more than one category, but only one is permalink category. I need to get the ID only of that permalink category (I need this info so I can take few latest posts from permalink category via custom query).
url looks like this http://domain.com/category-name/post-title
I need that "category-name" ID.
A Good one to use is:
<?php $page_object = get_queried_object(); ?>
<h1><?php echo $page_object->cat_name; ?></h1>
global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) ); get url
$url_array = explode('/',$current_url);
$retVal = !empty($url_array[5]) ? $url_array[5] : $url_array[4] ;
$idObj = get_category_by_slug($retVal);
echo $idObj->name
My answer is:
function get_category_by_url($url) {
foreach( (get_the_category()) as $category) {
if ( get_category_link($category->cat_ID) == $url )
return $category->slug;
}
return false;
}
$category = end(get_the_category());
$current = $category->cat_ID;
echo 'id='.$current . ' - name=' . $category->cat_name;
If the post belongs to many categories, what if you're viewing the post from a second category. In that case retrieving the category ID of the permalink category may not help, since you would need the related posts of the current category in action.
For that, you can get the ID by passing the category name as follows:
<?php get_cat_ID($cat_name)?>
Does this help?
Wordpress chooses the oldest category as the permalink category. There's no way to change that behavior unless you use some plugin. If you choose to use a plugin you make take the category ID from plugin settings.
You can list all categories of this post and choose most relevant category.
Use the following code inside The Loop:
foreach((get_the_category()) as $category)
{
if ( $category->cat_ID == 1000 )
; // DO SOMETHING
}