I am getting a list of all the images the following way:
$the_query = new WP_Query( array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit',
'posts_per_page' => -1,
) );
But I'm trying to get only those who are uploaded to a post on my website.
Not images attached to a specific post but exclude those who aren't featured on any post.
All you have to do is paste the following code inside a loop.
$args = array(
'post_parent' => get_the_ID(), // your post id
'post_type' => 'attachment',
'numberposts' => -1, // show all
'post_status' => 'any',
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$images = get_posts($args);
if($images) { ?>
<img src="<?php echo wp_get_attachment_url($image->ID); ?>" />
<?php
}
?>
I think the post_mime_type is not correct.
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_mime_type' => 'image/gif',
);
$query = new WP_Query( $args );
Hope this helps!
Related
I am making a new WP_Query to search for a product.
This shows me the wp_post information but not the stock information.
How can I order the stock results on top and the non-stock ones last in the search.
This is my current code.
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
's' => $filter_name,
'tax_query' => $tax_query,
'posts_per_page'=> $limit
);
$list = new WP_Query( $args );
Try this code but it doesn't work
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
's' => $filter_name,
'tax_query' => $tax_query,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => '_stock',
'posts_per_page'=> $limit
);
$list = new WP_Query( $args );
<?php
$pages = get_posts( array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'post_date',
'orderby' => 'meta_value_num',
'posts_per_page' => 4
) );
echo $post_id = $pages->ID ;
echo $post_thumbnail_id = get_post_thumbnail_id( $post_id );
?>
In above, code I want to skip my first post and display rest of.
Try this :
use offset parameter of get_posts() argument.
$pages = get_posts( array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'post_date',
'orderby' => 'meta_value_num',
'posts_per_page' => 4,
'offset'=>'1' //USE OFFSET PARAMETER
) );
//echo '<pre>';
//print_r($pages); //Retrieve post lists
echo $post_id = $pages->ID ;
echo $post_thumbnail_id = get_post_thumbnail_id( $post_id )
I've made a custom post type type called "Videos" and within that post type a custom category called "Crystal" while using this plugin(https://wordpress.org/plugins/video-thumbnails/ to generate the YouTube videos thumbnail into the post.
I'm trying to pull through all of the Crystal posts and only display the video thumbnail on the page with a permalink to the post.
Here is my code;
<div class="block" id="home-three">
<p>YouTube</p>
<?php
$args = array(
'post_type' => 'videos',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'post_parent' => 0,
'tax_query' => 'crystal',
);
$count = 1;
?>
<?php $video_query = new WP_Query( $args ); ?>
<?php while ( $video_query->have_posts() ) : $video_query->the_post(); ?>
<div>
<a href="<?php the_permalink(); ?>">
<?php if( ( $video_thumbnail = get_video_thumbnail() ) != null ) { echo "<img src='https://wordpress.org/plugins/video-thumbnails/" . $video_thumbnail . "' />"; } ?>
</a>
</div>
<?php wp_reset_query(); ?>
</div>
According to documentation, the tax_query parameters accepts an array.
So your WP_Query arguments should be:
<?php
$args = array(
'post_type' => 'videos',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'post_parent' => 0,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'crystal'
)
)
);
?>
You could also use the Category Parameters:
<?php
$args = array(
'post_type' => 'videos',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'post_parent' => 0,
'category_name' => 'crystal'
);
?>
I am trying to make a simple query in wordpress ordered by a meta_value_num of a custom field.
$args1 = array(
'post_type' => 'task',
'post_status' => 'publish',
'meta_key' => 'task_due_date',
'orderby' => 'meta_value_num',
'order' => ASC,
'posts_per_page' => -1,
);
I want to include the posts that have the custom field empty or null too ordered first or last in the query. How can I achieve that?
A simple and quick solution:
<?php
setup_postdata( $GLOBALS['post'] =& $post );
$posts= get_posts(array(
'post_type' => 'task',
'post_status' => 'publish',
'meta_key' => 'task_due_date',
'orderby' => 'meta_value_num',
'order' => ASC,
'posts_per_page' => -1,
));
if( $posts ):
foreach( $posts as $post ): ?>
<!-- HTML HERE -->
<?php endforeach;
wp_reset_postdata();
endif; ?>
?>
If you want to get the generated SQL just pass it to the WP_Meta_Query object:
$query_args = array(
'post_type' => 'task',
'post_status' => 'publish',
'meta_key' => 'task_due_date',
'orderby' => 'meta_value_num',
'order' => ASC,
'posts_per_page' => -1,
);
$meta_query = new WP_Meta_Query();
$meta_query->parse_query_vars( $query_args );
$res = $meta_query->get_sql(
'task',
$wpdb->posts,
'ID',
null
);
I am looking for an equivalent of getting a page/post with WP Query or get_posts e.g.:
$args=array(
'category' => 1,
'name' => 'my-page',
'post_type' => 'page',
'post_status' => 'publish',
'numberposts' => 1
);
$my_post = get_posts($args);
but I want this for a preview, only thing I have are the preview GET parameters:
?preview=true&preview_id=5&preview_nonce=b0d41a7fdb
The preview_id is actually the ID of the created post, not the id of the preview.
Any ideas?
So I eventually found a possible answer - this should retrieve the latest preview or "revision":
$args = array(
'post_status' => 'any',
'post_parent' => intval($_GET['preview_id']),
'post_type' => 'revision',
'sort_column' => 'ID',
'sort_order' => 'desc',
'posts_per_page' => 1
);
$my_post = get_posts($args);