wordpress wp_query get post from the n-th - php

in my blog I have done a sidebar where are displayed some articles. It is done by querying wordpress:
$pq = new WP_Query(array( 'post_type' => $ptype, 'showposts' => $pshow ));
My question is: how can I get retrieve articles from the 6-th, and not from the last posted?
Thank you so much.
In alternative, how can I extract random post different from the last six post?
Thanks in advance.

Add 'offset' => n to your query to bounce over the first n results.

Related

Wordpress Query Posts with date as a custom field

I have multiple post types - 'news', 'events' and 'others'. I want to combine them in one query - sorted by the date of creation. At the moment the following query provides the result:
query_posts( array(
'post_type' => array('news', 'events', 'others'),
'showposts' => 4,
'order' => 'DESC'
) );
The post type 'events' have a custom field which is called 'cf_date'. This field contains the date of the event. The posts of the post type 'events' should first appear 10 days before the start. How is it possible, that the events can be created flexible and will be displayed (added to the query) 10 days before the event?
Thank you for your help!
Create a template named archive-events.php, copying the existing archive.php in your theme. Inside the loop, insert your test for the date, using get_post_meta on the date field. Set the template on Page Attributes to the name you used for archive-events.php.

Wordpress sort by value from specific table

I'm trying to get 4 posts in order by 'pageviews' from a plugin named Wordpress Popular posts. I found in phpmyadmin where it stores the page views per post. My problem is that the values for 'pageviews' for the post is not in the wp_postmeta but in a separate table: wp_popularpostdata and named pageviews. Please help :)
phpmyadmin
from wp_popularpostdata
postid | pageviews
..1..............14
the code below I'm trying returns nothing
$args = array(
'posts_per_page' => 4,
'meta_key' => pageviews,
'orderby' => meta_value_num,
'order' => DESC,
);
$my_query = new WP_Query($args);
You could use the built-in function wpp_get_mostpopular as explained here:
if (function_exists('wpp_get_mostpopular'))
wpp_get_mostpopular("range=weekly&order_by=comments");
If it isn't sufficient to you, you'll have to alter the query through wordpress filters like posts_join, posts_where and etc. The docs should help you to find the way.

How i can sort post with current user city in wordpress

I am working on site where i want to sort the books (Custom post type) with user current city. For example if my city is Sialkot and there are three books with custom fields Sialkot show them on top. What i have already done is
Store user city as custom field
Get the current user city
Only need help for sorting data. Currently i am sorting with meta key and meta value.
Thanks in advance.
You can simply add the below code and thats all
query_posts('&meta_key=popularity&orderby=meta_value');
Here popularity is the name of the custom field
The better way to get sorted the current user city as below
$args = array(
'post_type' => 'books',
'meta_key' => 'user_city', // SPECIFY YOUR CUSTOM FIELD SLUG
'orderby' => 'meta_value',
'order'=> 'DESC'
);
$posts = get_posts( $args );
Let us know if you still face any issue, Mentioned over comments :)
We are ready to help you.

Get posts order by meta_value_num with two meta keys

I want to get post by get_post order by meta_value_num and meta keys , I tried this code:
get_posts(
array( 'post_type' => 'posts',
'order' => 'ASC',
'orderby' => 'meta_value_num',
'meta_query' => array(
array('key' => 'class'),
array('key' => 'chair')
)
)
);
I want to get the post sort by class's number then chair's number .
but htis code not working how can this be done?
Although the new orderby parameter is great in WP_Query, it does not support multiple orderby for multiple meta_key's.
I've went through a couple of scenarios and even went and digged into trac and make and came up with the following
make.wordpress.org A more powerful orderby in wordpress 4.0
trac ticket #17065
None of the issues regarding this very problem have been answered. It also seems from those two links that there is an issue ordering by a meta_key and another field like post date for instance. I haven't tried this as yet.
I truelly think that you have two choices here without creating two queries.
hack using the posts_orderby filter as described by #s_ha_dum in his answer here
By making use of PHP ordering using usort. The idea here would be to sort your query by one meta_key and then take the returned posts array ($posts) and sort them by the second meta_key before the loop starts. You can either use the the_posts filter (just remember to remove the filter once done) or simply unset $posts in your template and set it with the reordered array of posts once done

Wordpress bbpress get last 2 posts php

I'm trying to build a wordpress query to get the last 2 posts from bbpress2.3.2 to display on the home page of the site and I just can't seem to grasp how to do this.
I have been digging about in the DB but still can't find any clues.
Can anyone give me a push in the right direction?
Thanks.
BBpress store everything in the wp_posts table, with different post_type.
So if you want to get the last 2 posts, you have the find the right post_type (it can be topic for topics, or reply for replies in the topics). Then you just have to use the Wordpress function get_posts :
$args = array(
'post_type' => 'reply',
'numberposts' => 2,
);
$postslist = get_posts( $args );
You can change the number of posts you want to get, and the type. With that you should have what you want :)
If you're blocked : http://codex.wordpress.org/Template_Tags/get_posts

Categories