How Can I Do LIMIT 1, 2 In WP_Query - php

I am using Wp_Query method and I want to learn is there any parameter to limit my query.
I try to use LIMIT 1,2 in WP_Query.
Thanks for advance!
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'ID',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'evet',
'compare' => '='
)
)
);
// The Query
$the_query = new WP_Query( $args );

you can use posts_per_page and offset args for WP_Query() like:-
$query = new WP_Query( array( 'posts_per_page' => 5, 'offset' => 3 ) );
for more :- http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
you are asking for LIMIT 1,2 (means offset,count(how many rows)) so
posts_per_page = count
offset = offset

Related

How to get most viewed posts using another table in Wordpress

I want to get my most viewed posts, and I'm using yuzo plugin for count views.. But this code doesn't work.. How can I make it work ?
$popular = new WP_Query( array(
'post_type' => array( 'post' ),
'showposts' => $instance['popular_num'],
'cat' => $instance['popular_cat_id'],
'ignore_sticky_posts' => true,
'orderby' => 'wpng_yuzoviews.views',
'order' => 'dsc',
'date_query' => array(
array(
'after' => $instance['popular_time'],
),
),
) );
Your query will be something like:
$query = new WP_Query( array(
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'posts_per_page' => 5
) );
By default, the ordering will be highest to lowest, thus giving you the "top" 5.

Count WP_Query returns 0 in Wordpress

Trying to implement a custom pagination in Wordpress and I cannot get the number of posts to dynamically create the pagination element.
$loop = new WP_Query(
array(
'post_type' => 'product',
'post_status ' => 'publish',
'orderby' => 'post_date',
'order' => 'date',
'posts_per_page' => $per_page,
'offset' => $start,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $suv_cates
)
)
)
);
$count = new WP_Query(
array(
'post_type' => 'product',
'post_status ' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $suv_cates
)
)
)
);
return $count->post_count;
The first query with the $loop returns the posts that I need. But when I return the $count or $count->post_count, it returns 0.
You need to reset the first query. So add this code after first loop and query
wp_reset_postdata();
wp_reset_query();
Then let me know the result.
Thanks
Posts per page is set to -1.
You could just count Loop?

Wordpress - Get posts by comparing ID

I'm trying to retrieve the posts in Wordpress that have post_id < $number , so I looked for it in the documentation, and tried this code :
$args = array(
'numberposts' => 10,
'posts_per_page' => 10,
'offset' => 0,
'orderby' => 'id',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
array(
'key' => 'post_id',
'value' => '3000',
'compare' => '<'
)
);
$query = new WP_Query( $args );
$posts = $query->get_posts();
but it didn't work (return the last 10 posts without comparing the ID), and I couldn't find an answer :(
So, could tell me please how to do it ..
And thanks in advance ..
Use the following query. it will return you array with your desired data.
global $wpdb;
$results = $wpdb->get_results( "SELECT ID, post_title FROM $wpdb->posts WHERE ID < 10;" );

Wordpress Query Args - Only show post with meta_value greater than 0

I have a wordpress loop with an array of arguments to show only specific posts (any posts with a deposit_amount value of 0).
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'cat' => '11',
'meta_key' => 'deposit_amount',
'meta_value' => 0
);
$loop = new WP_Query( $args );
?>
I would like to create a similar array but showing posts with a deposit_amount meta_value of greater than 0
i have tried to use the php greater than operator but breaks the code.
'meta_value' => >0
Can anyone point me in the right direction with this problem?
just found 'meta_compare' => '>'
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'cat' => '11',
'meta_key' => 'deposit_amount',
'meta_value' => 0,
'meta_compare' => '>'
);
$loop = new WP_Query( $args );
?>
Use a Meta Query:
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'cat' => '11',
'meta_query' => array(
array(
'key' => 'deposit_amount',
'value' => 0,
'compare' => '>'
)
)
);

get posts equal to two meta_values

I'm trying to retrieve all posts where the meta_key status is equal to either correct or wrong. at the moment i've created below php variable however it shows all posts also posts where the meta_key is not equal to correct or wrong. What am i doing wrong?
$args = array(
'numberposts' => -1,
'post_status' => 'publish',
'cat' => '4,5,6',
'meta_query' => array(
array(
'meta_key' => 'status',
'meta_value' => 'wrong'
),
array(
'meta_key' => 'status',
'meta_value' => 'correct'
)
)
);
$the_query = new WP_Query( $args );
Try this:
$args = array(
'numberposts' => -1,
'post_status' => 'publish',
'cat' => '4,5,6',
'meta_query' => array(
array(
'meta_key' => 'status',
'meta_value' => array('wrong','correct')
'compare' => 'IN'
)
)
);

Categories