How can I skip the first post in WordPress? - php

How can I skip the first post in WordPress?
<?php
$recentPosts = new WP_Query();
$recentPosts->query(array('showposts' => 6,'post_type' =>array('stiri')));
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>

Use the offset parameter:
<?php
$recentPosts = new WP_Query( 'offset=1' ) );
$recentPosts->query(array('showposts' => 6,'post_type' =>array('stiri')));
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>

Use the offset
$recentPosts = new WP_Query (
array(
'post_type' => 'stiri',
'post_status' => 'publish',
'posts_per_page' => 6, // all = -1
'orderby' => 'date',
'order' => 'DESC',
'offset' => 1
)
);

Related

Get Recent and Custom posts from same wordpress loop

I am using this code from which I am able to get 3 recent posts.
$query = new WP_Query( array( 'post_type' => 'property', 'posts_per_page' => 3, 'post_status' => 'publish', 'order' => 'DESC'));
and I am using this code to get 3 custom posts with the post__in method.
$query = new WP_Query( array( 'post_type' => 'property', 'posts_per_page' => 3, 'post_status' => 'publish', 'order' => 'DESC', 'post__in' => array( 10244, 7177, 8262)));
How can I combine them to get the 3 recent and 3 custom posts from one loop?
Any Help Appreciated. Thanks.
You can do it by merging their posts into one query.
f.e.
$merged_query = new WP_Query();
$merged_query->posts = array_merge( $query1->posts, $query2->posts );
Example:
<?php
$merged_query = new WP_Query();
$merged_query->posts = array_merge( $query1->posts, $query2->posts );
while ( $merged_query->have_posts() ) : $merged_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>

Current Year Post type Wordpress

I've a this query post
<?php
$args = (array(
'post_type' => 'issue_number',
'posts_per_page' => 13,
'paged' => $paged
));
// query
$the_query = new WP_Query( $args );
?>
<?php while( $the_query->have_posts() ) : $the_query->the_post();?>
bla bla
<?php endwhile; ?>
I want to display only current year post type (issue_number)...how to do?
This should do it.
$current_year = the_date( 'Y' );
$args = array(
'post_type' => 'issue_number',
'posts_per_page' => 13,
'paged' => $paged,
'year' => $current_year
);

Manipulating a PHP variable inside WP_Query in WordPress

* This question may be quite long so hope to bear with me *
Was trying to group address under a particular city dynamically.
Layout sample:
So based on the sample layout above, Cupertino belongs to CA and so is displayed below it.
The city is selected inside an ACF group.
I tried to query the cities by the following code:
<?php
$counter = 0;
$cityArray = array();
?>
<?php $loop = new WP_Query( array( 'posts_per_page' => -1, 'post_type' => 'branches_locations', 'orderby'=>'post_id', 'order'=>'ASC' ) ); ?>
<?php while( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$city = get_field('city');
if(!in_array($city, $cityArray))
{
$cityArray[$counter] = get_field('city');
$counter++;
}
?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php
foreach ($cityArray as $i)
{
$loopCity = $i;
?>
<?php echo "city before loop is $i"; ?>
<?php $loop = new WP_Query( array( 'posts_per_page' => -1, 'post_type' => 'branches_locations', 'meta_key' => 'city', 'meta_value'=> '$i', 'order' => 'ASC' ) ); ?>
<?php echo "city after loop is $i"; ?>
The value of the $i variable do display as the sample below shows:
However, the WP_Query seems to not recognize the variable $i.
If I used the following:
<?php $loop = new WP_Query( array( 'posts_per_page' => -1, 'post_type' => 'branches_locations', 'meta_key' => 'city', 'meta_value'=> 'Los Angeles', 'order' => 'ASC' ) ); ?>
instead of:
<?php $loop = new WP_Query( array( 'posts_per_page' => -1, 'post_type' => 'branches_locations', 'meta_key' => 'city', 'meta_value'=> '$i', 'order' => 'ASC' ) ); ?>
The query outputs all address under Los Angeles.
What could I be doing wrong?
Seems that you're passing on a variable as string.
Try:
$loop = new WP_Query( array( 'posts_per_page' => -1, 'post_type' => 'branches_locations', 'meta_key' => 'city', 'meta_value'=> $i, 'order' => 'ASC' ) );

Displaying post with certain key value

I have a wordpress function that displays all posts of a custom meta.
PHP:
<?php
$args = array(
'post_type' => 'todo_listing',
'posts_per_page' => 4,
'order' => 'asc'
);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
echo get_the_ID();
endwhile;
?>
This displays 4 posts per page. However, I only want to display those posts whose $key value is dogs.
Hope this helps.
$args = array(
'post_type' => 'todo_listing',
'posts_per_page' => 4,
'order' => 'asc',
'meta_value' => 'dogs'
);

PHP: Doing while loop if "if" statement is true

I am trying to show only those events, which are in current date or in future. To show events I have such code:
<?php
$wp_query = new WP_Query();
global $more;
// set $more to 0 in order to only get the first part of the post
$more = 0;
$wp_query->query( array( 'post_type' => 'events', 'posts_per_page' => 7, 'paged' => $paged, 'orderby' => 'menu_order', 'order' => 'ASC') );
while ( ($wp_query->have_posts()) ): $wp_query->the_post();
get_template_part( 'content', 'event' );
endwhile;
?>
I tried to add "IF statement" to compare event start date with current date, but it doesn't seem to work. I have tried a lots of versions, but this one I think should work:
$wp_query->query( array( 'post_type' => 'events', 'posts_per_page' => 7, 'paged' => $paged, 'orderby' => 'menu_order', 'order' => 'ASC') );
$OstartDate = get_post_meta($post->ID, '_event_start', TRUE);
$today = date('d.m.Y');
while ( ($wp_query->have_posts()) && ($OstarDate < $today) ):
$wp_query->the_post();
From the second one you should try,
$OstartDate=get_post_meta($post->ID,'_event_start',TRUE);//check it will return a number
if(time() > $OstartDate)
{
while ($wp_query->have_posts()):
$wp_query->the_post();
.....
}
a7-simple-events may help you.
You can compare the posts with postmeta using meta_query
$today = date('d.m.Y');
$args=array( 'post_type' => 'events', 'posts_per_page' => 7, 'paged' => $paged, 'orderby' => 'menu_order', 'order' => 'ASC', 'meta_query' => array(
array(
'key' => '_event_start',
'value' => $today,
'type' => 'date',
'compare' => '<'
)
)) ;
$wp_query = new WP_Query( $args );
while ( ($wp_query->have_posts()) ):
$wp_query->the_post();
endwhile;

Categories