WordPress - get the 4th post in a query - wordpress

I have a WordPress query like this :
<?php $recent = new WP_Query( array( 'tag' => $tags, 'posts_per_page' => '4' ) ); while( $recent->have_posts() ) : $recent->the_post(); ?>
Which gives me the last 4 posts using the $tags.
But any idea of how may I edit this code to do not get the four first posts but from the 4th?
Thanks!

Take a look at the WP_Query Documentation. It goes into detail on how to properly interact with the class.
If you want to query your posts starting from the 4th post, you'll want to take a look at the offset parameter. In your case, take a look at the code below (Note: I moved the arguments array to a variable for clarity)
$recent_args = array(
'tag' => $tags,
'posts_per_page' => 4, // Don't need quotes around integers
'offset' => 3, // Add this param to "Skip" this many posts
);
$recent = new WP_Query( $recent_args );
// Loop through your posts here

If you need to get all posts beyond the first (newest) posts, you can use the offset argument:
<?php $next_posts = new WP_Query( array(
'tag' => $tags,
'offset' => 4,
)
);
while($next_posts->have_posts()) : $next_posts->the_post(); ?>
There is a caveat to this! If you need pagination or set posts_per_page to -1, this will not work. For more information on a more robust solution if you need pagination, checkout the WP documentation:
https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination

So here is the code that worked for me :
<?php $recent = new WP_Query(array( 'tag' => $tags, 'posts_per_page' => '4', 'offset' => '3', )); while($recent->have_posts()) : $recent->the_post(); ?>
Thanks a lot to #disinfor & #Xhynk for leading me to the right direction !

Related

Sticky posts not showing when using category filter

I am trying to show a specific category post on my home page and in which 1 post is sticky that will appear first but its not working.
I have noticed when I try to show all posts, then sticky post shows first. When I try to show a specific category then its not showing first.
Here is my code:
$sticky = get_option( 'sticky_posts' );
$args = array( 'posts_per_page' => intval($blogtoShow),
'post_status'=>'publish',
'post_type'=>'post',
'cat' => $cattoShow,
'orderby'=>'date',
'post__in' => $sticky);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) :
while( $the_query->have_posts() ) : $the_query->the_post();```
You're looking for the following:
ignore_sticky_posts (boolean) – ignore post stickiness (available since version 3.1, replaced caller_get_posts parameter). false (default): move sticky posts to the start of the set. true: do not move sticky posts to the start of the set.
If you're using a pre-made theme the default might have been modified.
You should add this to your arguments array: 'ignore_sticky_posts' => 0.
A comma should separate each arguments. (Not tested but should work)
More info on the worpress query: https://developer.wordpress.org/reference/classes/wp_query/
----------
EDIT 1.1: I think you need to display a specific template for the sticky (as it's not considered a normal post). At the begining of your loop can you try the following?
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 3,
'post__in' => $sticky,
);
$query = new WP_Query( $args );
if ( $sticky[0] ) {
// insert sticky template...
} else {
// insert posts template...
}

WP Query Get Posts by Comment Author ID and specific keyword?

I am trying to create a custom loop which gets the posts that author__in 1 has commented the specific keyword 'test' on and use that to build the recent posts list.
<?php $query_args = array('search' => 'test', 'author__in' => '1', 'post_status' => 'publish', ); $the_query = new WP_Comment_Query( $query_args );?> <?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
This query doesn't work, it gives me nothing, i guess ->have_posts does not exist for wp_comment_query?
How can i build a query using WP_query that lists the most recent posts with comment 'test' from author with id 1? Is it even possible?
Couple things here.
First, the "search" parameter is actually only "s", not the full word. Here's that reference: https://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter
Second, the author__in param takes an array, like this: $query = new WP_Query( array( 'author__in' => array( 2, 6 ) ) ); rather than a single integer or quoted digit. Here's that reference: https://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
I think you could get what you want by just changing your code slightly. One thing to note, I'm not sure if the search parameter does a keyword search in comments though, it might only search post content. Try this:
$query_args = array('s' => 'test', 'author__in' => array(1), 'post_status' => 'publish', );
Let me know if this works for you.

How to have the title of the last post in a static page?

I have a page for posts and a different static home page, as well as other common pages, eg About, Sponsors.
I am trying to have in the header of all pages the name of the site and after the title of the last post. I have tried different functions (the_title(), get_the_title()). It works well in the post page, but for all the remaining I get the title of the page ("About" or "Home" or "Sponsors").
I get that the functions I used so far might only work inside the loop, but was wondering if there's some global way to have the title of the last post available everywhere.
I got to: $posts_page = get_option( 'page_for_posts' ) to get the id of the post page and was trying to find a way from there to get the title of the last post, but no success so far.
Is there a simple way to do this?
Try it:
echo current( wp_get_recent_posts( array('numberposts' => 1, 'post_status' => 'publish') ) )['post_title'];
Do you mean the latest/most recent post?
Would this work for you?
<?php
$args = array(
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC'
);
$post = get_posts( $args );
$latest_post_title = $post[0]->post_title;
echo $latest_post_title;
?>
Or maybe this is a bit more light weight, not sure but they both do pretty much the same thing.
<?php
$args = array(
'numberposts' => 1,
);
$recent = wp_get_recent_posts($args);
$last_post_title = $recent[0]['post_title'];
echo $last_post_title;
?>

Get last post date for custom post type - Wordpress

This is probably a simple one but I can't seem to find an answer anywhere.
I have 2 custom post types. I display them in sections on my home page.
In each section I'd like to have "Last Updated [DATE]" text near the title for each section.
I've found <?php get_lastpostdate( $timezone ) ?> but is there a way of specifying which post type you'd like to query?
[UPDATE]
Here's the final code I used based on Howdy_McGee's answer below. I wanted the date to read as "16th May" for example.
`Thanks, that's the route I started to go down as well. I guess I was hoping to not do another WP_Query, but it works. This is the final code I used:
<p class="right last-update"><?php
$latest = new WP_Query(array('post_type' => 'car', 'post_status' => 'publish', 'posts_per_page' => 1, 'orderby' => 'modified', 'order' => 'ASC'));
if($latest->have_posts()){
$modified_date = $latest->posts[0]->post_modified;
}
//Get the last post update and display the date as "10th March"
$lastpost = strtotime($modified_date);
$lastmonth = date('F', $lastpost);
$lastday = date('jS', $lastpost);
echo 'Last Updated '.$lastday.' '.$lastmonth;
?>
</p>
You can use the_post_modified() function if you're in The Loop. The modified date will change any time the post is changed in any way / updated in any way.
Update
Ok, lets run a small query, what this does is pulls the latest post, modified or new. Since it's just 1 post we can just check if it has posts, and get the first posts modified date.
<?php
$latest = new WP_Query(
array(
'post_type' => 'car',
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => 'modified',
'order' => 'DESC'
)
);
if($latest->have_posts()){
$modified_date = $latest->posts[0]->post_modified;
}
?>
For a full list of Date Format Options, View Codex. If you're using it outside The Loop, you can use get_the_modified_date() function. Hope it helps!
I needed something similar for a site I was working on and adapted your code slightly if this helps anyone else
<?php
$taxonomy = 'book'; // or post
$latest = new WP_Query(
array(
'post_type' => $taxonomy,
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => 'modified',
'order' => 'DESC'));
if($latest->have_posts()){
echo 'Last Updated ' . mysql2date('jS F Y', $latest->posts[0]->post_modified);
}
?>
This shows the last modified date of the post_type selected
Update: just realised that someone else posted this answer, I only looked at the 1st post "update", but, meh

wordpress post gallery functions does not show the number of posts

hi i have this code i created in content-gallery.php
but when i use printf to show the number of photos within the post does not show it
it must be print
[This Post Gallery Contains 9 Photos.]
but does not work. why?
when my errors?
<?php
$the_images = get_children( array(
'post_parent' => $post->ID, // Pass the ID of a post or Page to get its children. Pass 0 to get attachments without parent. Pass null to get any child regardless of parent.
'post_mime_type'=> 'image', // A full or partial mime-type, e.g. image, video, video/mp4, which is matched against a post's post_mime_type field.
'post_type' => 'attachment',
'orderby' => 'menu_order',
'order' => 'ASC',
'numberposts' => 999
));
if ( !empty($the_images) ) {
$the_total_images = count($the_images);
$the_images = array_slice($the_images, 0, 10); // prin_r form 0 to 10 images
?>
<p><strong>
<?php printf (_n('This Post Gallery Contains %s Photo.', 'This Post Gallery Contains %s Photos.', $the_total_images, 'the-bootstrap'), $the_total_images); ?>
</strong></p>
<?php } ?>
Try using sprintf() instead of printf().
Also, make sure to not have any spaces when using functions. spaces are alright once your within the brackets, but have the space in printf () is bad practice and most of the time errors or doesn't work.
You're checking if the_images is not empty. If it is empty then $the_total_images is never set.
I don't understand your usage of array_slice either. Surely it would be more efficient to only get 10 posts.
Here's how I'd do it:
<?php
global $post;
$the_images = get_posts( array(
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_type' => 'attachment',
'post_status' => 'any',
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1,
) );
$the_total_images = ( $the_images ) ? count( $the_images ) : 0;
$image_count_text = sprintf( _n( 'This Post Gallery Contains %s Photo.', 'This Post Gallery Contains %s Photos.', $the_total_images, 'the-bootstrap' ), $the_total_images); ?>
<p><strong><?php echo $image_count_text; ?></strong></p>
$the_total_images is no longer dependent on the outcome of get_posts(). I've left it loading all image attachments but I think a better solution could be found if you're only going to use 10.

Resources