What is the function got get all the media files wordpress? - wordpress

Can anyone suggest me what is the function to get all the images stored for wordpress? I just need to list all the images seen under menu Media of the wordpress admin.
Thanks in advance

Uploaded images are stored as posts with the type "attachment"; use get_posts() with the right parameters. In the Codex entry for get_posts(), this example:
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => null, // any parent
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $post) {
setup_postdata($post);
the_title();
the_attachment_link($post->ID, false);
the_excerpt();
}
}
?>
...loops through all the attachments and displays them.
If you just want to get images, as TheDeadMedic commented, you can filter with 'post_mime_type' => 'image' in the arguments.

<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$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 '<li>';
echo wp_get_attachment_image( $attachment->ID, 'full' );
echo '<p>';
echo apply_filters( 'the_title', $attachment->post_title );
echo '</p></li>';
}
}
endwhile; endif; ?>
</ul>

Related

Get a list of meta values and search for posts Wordpress

how to link meta_values to make a search for posts with the current meta?
The code above, show in a dropdown all the meta_value of city meta_key and cpt post type
<?php if ( have_posts() ) : ?>
//some code for title
<?php
$args = array(
'post_type' => 'cpt',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => 'city',
);
$dbResult = new WP_Query($args);
echo '<ul class="menu dropdown-menu">';
while ( $dbResult->have_posts() ) : $dbResult->the_post();
$mykey_values = get_post_meta($id, 'city', false);
foreach ( $mykey_values as $key => $value ) {
$by_link = esc_url(add_query_arg(array( 'b' => $value ))); ?>
<li> <?php echo $value ?></li>
<?php }
endwhile; ?>
<?php wp_reset_query(); ?>
<?php
echo '</ul>'; ?>
</nav>
My problem is how to get a link of the values and search.
So to get a link, I tried
$by_link = esc_url(add_query_arg(array( 'b' => $value ))); ?>
<li> <?php echo $value ?></li>
And to search I made another loop and tried to get b value:
<?php
$meta_value = $_GET['b'] != '' ? $_GET['b'] : '';
$args2 = array(
'post_type' => 'cpt',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'city',
'value' => $meta_value,
'compare' => 'LIKE',
),
),
);
$dbResult2 = new WP_Query($args2);
while ( $dbResult2->have_posts() ) : $dbResult2->the_post(); ?>
<?php get_template_part( 'content', get_post_type() ); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
Any help???
If i understand you correct then you want to get a URL for each dropdown option. If so, you can do something like this-
echo '<ul class="menu dropdown-menu">';
while ( $dbResult->have_posts() ) : $dbResult->the_post();
$mykey_values = get_post_meta($id, 'city', false);
foreach ( $mykey_values as $key => $value ) {
$by_link = esc_url(add_query_arg(array( 'b' => $value ))); ?>
<li data-url="<?php echo $by_link;?>"><?php echo $value ?></li>
<?php }
endwhile;
wp_reset_query();
echo '</ul>';
You can extend any element with 'data-' attributes. You can add any data attribute you wish- data-bla, data-blabla, data-whatever.
When you need that data, you can use jQuery to retrieve it-
(function() {
$('.my-menu li').click(function() {
var li_url = $(this).attr('data-url');
// Do something here...
}
}

Get pages with specific custom field value not working

I have created a custom field in some pages and I need to loop through these pages and print their info. The code I'm using isn't working (the foreach is not looping).
Here's the code:
<?php
$args = array(
'meta_key' => 'categoria-pagina',
'meta_value' => 'programas'
);
$pages = get_pages($args);
foreach ($pages as $page) {
echo "<p>$page->post_title</p>";
}
wp_reset_postdata();
?>
And here's the page custom field config (wordpress in portuguese):
What's wrong with it?
Solved with this code:
<?php
$args = array(
'post_type' => 'page',
'meta_key' => 'categoria-pagina',
'meta_value' => 'programas'
);
$myPages = new WP_Query($args);
while ($myPages->have_posts()) : $myPages->the_post();
echo "$post->post_title";
endwhile;
wp_reset_postdata();
?>
<?php
$args = array(
'meta_key' => 'categoria-pagina',
'meta_value' => 'programas'
);
$custom_query = new WP_Query( $args );
// The Loop
if ( $custom_query->have_posts() ) {
while ( $custom_query->have_posts() ) {
$custom_query->the_post();
echo '<p>' . get_the_title() . '</p>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
That should do the trick for you.

only first caption showing for attachment in Wordpress

ok I'm using the below code to generate caption for my attachments. The problem is if I have multiple images in a single page, the caption for first image shows for all the images. like if I have "Text One" as a caption for my image one, all the image showing this "Text One" caption. How can I solve this problem?
<?php
$args = array( 'post_type' => 'attachment',
'orderby' => 'menu_order',
'order' => 'ASC',
'post_mime_type' => 'image' ,
'post_status' => null,
'numberposts' => 50,
'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$caption = $attachment->post_excerpt;
}
}
?>
<p class="project-caption"><?php echo $caption ?></p>
currently there is a slider, which shows the images. & the slider code is:
<?php
if($repeater):
foreach($repeater as $r):
?>
<li class="<?php echo $r["fit_to_screen"] ? "img_fit" : ""; ?>">
<?php
if($r["acf_fc_layout"] == "image"):
$html = "<img data-fit='".$r["fit_to_screen"]."' src='".$r["image"]["url"]."' alt='".$r["image"]["alt"]."'/>";
echo apply_filters( 'post_thumbnail_html', $html, $post->ID , $r["image"]["id"], "large" , array("alt"=>$r["image"]["alt"]) );
else:
echo getVideoEmbed($r["video_url"]);
endif;
?>
</li>
<?php
endforeach;
endif;
?>
You override the $caption in the loop so at the end you get the last caption.
You can print the $caption inside the loop and then you get all the captions.
foreach ( $attachments as $attachment ) {
$caption = $attachment->post_excerpt;
echo '<p class="project-caption">'.$caption.'</p>';
}
You get the same caption because you are displaying the caption after the foreach, and you should do it in the foreach:
<?php
$args = array( 'post_type' => 'attachment',
'orderby' => 'menu_order',
'order' => 'ASC',
'post_mime_type' => 'image' ,
'post_status' => null,
'numberposts' => 50,
'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$caption = $attachment->post_excerpt;
echo '<p class="project-caption">' . $caption . '</p>';
}
}
?>

how to get related posts list in home page

how can i get list of latest posts with their relative posts by tags?
example:
Latest post post title 1
related post
related post
Latest post post title 2
related post
related post
use this in Index.php
<?php
$args = array(
'numberposts' => 100,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' =>,
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true );
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
<ul>
<?php
foreach( $recent_posts as $recent )
{
echo '<li>'.$recent["post_title"];
$tags = wp_get_post_tags($recent["ID"]);
if ($tags)
{
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($recent["ID"]),
'posts_per_page'=>100,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() )
{
echo '<ul>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php
endwhile;
echo '</ul>';
}
wp_reset_query();
}
echo '</li>';
}
?>
</ul>
you can do something like this
your main loop contain recent post so during each loop use following function to get it's tag
$tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
then you can use another loop of using those tags
$query = new WP_Query( 'tag_id='.$tag_ids );
now $query has content you want.

How do I show the number of images attached to a post on the image attachment page?

I use the image attachment page to show images attached to a post one by one, in a slideshow sort of affect. I'd like to be able to display the total number of images attached to the parent post and the number of the particular image that's being shown on any given attachment page so you see the picture and the words "Image 3 of 15" for example.
Update... I was able to get the total number to show using this code:
<?php
global $post;
$attachments = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );
$count = count( $attachments );
echo $count;
?>
I still can't figure out how to show the number of the current image.
Anyone have any suggestions?
Update 2...
Greenie's answer got me almost there but it's outputting all the numbers at once:
"Image 1 of 8Image 2 of 8Image 3 of
8Image 4 of 8Image 5 of 8Image 6 of
8Image 7 of 8Image 8 of 8"
Here is the code I used:
global $post;
$attachments = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );
$count = count( $attachments );
$currentImage = 1;
foreach ($attachments as $attachment) {
// output your image here
echo "Image ". $currentImage . " of ". $count;
$currentImage++;
}
What's going wrong?
Update 3 - THE ANSWER!
global $post;
$attachments = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );
$count = count( $attachments );
$specific = array();
$i = 1;
foreach ( $attachments as $attachment ) {
$specific[$attachment->ID] = $i;
++$i;
}
echo "Image {$specific[$post->ID]} of {$count}";
This works:
global $post;
$attachments = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );
$count = count( $attachments );
$specific = array();
$i = 1;
foreach ( $attachments as $attachment ) {
$specific[$attachment->ID] = $i;
++$i;
}
echo "Image {$specific[$post->ID]} of {$count}";
Add something like this to the above code:
$currentImage = 1;
foreach ($attachments as $attachment) {
// output your image here
echo "Image ". $currentImage . " of ". $count;
$currentImage++;
}
If you are looking for a plugin to manage image gallery, you can use attachments plugin,
http://wordpress.org/plugins/attachments/
It keeps the gallery separate and does not put the image gallery shortcodes in post content, thus providing you with full hold over image display in your post/page/custom post. You can also change the order of your images by just drag-n-drop
here is a sample code of how to retrieve your gallery images,
<?php $attachments = new Attachments( 'attachments' ); /* pass the instance name */ ?>
<?php if( $attachments->exist() ) : ?>
<h3>Attachments</h3>
<p>Total Attachments: <?php echo $attachments->total(); ?></p>
<ul>
<?php while( $attachments->get() ) : ?>
<li>
ID: <?php echo $attachments->id(); ?><br />
Type: <?php echo $attachments->type(); ?><br />
Subtype: <?php echo $attachments->subtype(); ?><br />
URL: <?php echo $attachments->url(); ?><br />
Image: <?php echo $attachments->image( 'thumbnail' ); ?><br />
Source: <?php echo $attachments->src( 'full' ); ?><br />
Size: <?php echo $attachments->filesize(); ?><br />
Title Field: <?php echo $attachments->field( 'title' ); ?><br />
Caption Field: <?php echo $attachments->field( 'caption' ); ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>

Resources