How can i skip my first post using wordpress? - php

<?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 )

Related

Get all images attached to any post

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!

How to pull through custom post type by custom taxonomy?

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'
);
?>

How can I show the tags for each post in the result of my query - WP

I have the following query:
$recent_posts = wp_get_recent_posts(array(
'post_type' => 'post',
'post','numberposts' => 9,
'post_status' => 'publish'
foreach($recent_posts as $post1) { $j++
and print the result in a html table.
echo $post1['post_title'];
echo $post1['post_content'];
but not how to display the tags associated with post
If You know the post_id You can use:
wp_get_post_tags( $post_id, $args )
More here: https://codex.wordpress.org/Function_Reference/wp_get_post_tags
i can use
the_title();
get_the_content();
the_tags('Tags: ',','); etc
previously declared:
$temp_query = $wp_query;
$args = array(
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'numberposts' => 9,
'post_status' => 'publish'
);
query_posts( $args );
if (have_posts())
while ( have_posts() ) : the_post();

WordPress: OrderBY meta_key_value including null value

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
);

How to simplify multiple WP_Query with different meta values

Their are 6 total queries I need to use, below is list of 2 of them. Is it possible to simplify it? As you can see the key is the different values of meta_value, they are 1-6.
<?php
$args1 = array(
'posts_per_page' => -1,
'post_parent' => $post->ID,
'post_type' => 'page',
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => 'product_column',
'meta_value' => 1
);
$the_query1 = new WP_Query($args1);
if ($the_query1->have_posts()) {
while ($the_query1->have_posts()) {
$the_query1->the_post();
//do stuff
}
}
wp_reset_postdata();
?>
<?php
$args2 = array(
'posts_per_page' => -1,
'post_parent' => $post->ID,
'post_type' => 'page',
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => 'product_column',
'meta_value' => 2
);
$the_query2 = new WP_Query($args2);
if ($the_query2->have_posts()) {
while ($the_query2->have_posts()) {
$the_query2->the_post();
//do stuff
}
}
wp_reset_postdata();
?>
If you just want to get all the posts with meta values 1-6, you can pass as array
$args1 = array(
'posts_per_page' => -1,
'post_parent' => $post->ID,
'post_type' => 'page',
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => 'product_column',
'meta_value' => array( 1,2,3,4,5,6 )
);
$the_query1 = new WP_Query($args1);
Edited as per comments:
You could add the values to an array and run a foreach loop
$meta_values = array( 1,2,3,4,5,6 );
foreach ( $meta_values as $meta_value ) {
$args = array(
'posts_per_page' => -1,
'post_parent' => $post->ID,
'post_type' => 'page',
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => 'product_column',
'meta_value' => $meta_value
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
//...
}
}
wp_reset_postdata();
}

Categories