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.
Related
I'd like to combine two php/mysql queries against the wp_posts table using the code below.
However, as you can see, there's an error there with the second post_type. The second post_type is 'post', and is never recognized. :-(
How would I make that an "AND", to gather BOTH the 'membercontent' and 'post' data ? The same question applies to the 'value' statement just below it. 'true' and 'yes'.
<?php $args = array(
'post_type' => 'membercontent', 'post',
'meta_query' => array(
array(
'key' => 'tt_freemium',
'value' => 'true', 'yes',
),
),
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => '200' );
`$ourposts = new WP_Query( $args );?>
Just figured it out. Duh. Had the answer all along....ARRAYS!
<?php $args = array(
'post_type' => array ('membercontent', 'post',),
'meta_query' => array(
array(
'key' => 'tt_freemium',
'value' => array ('true', 'yes',),
),
),
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => '200' );
$ourposts = new WP_Query( $args );?>
So I've been looking around here and other sites for a solution. I found lots of really helpfull posts but for some reason I just cant get this to work.
What I have:
WP posts with custom fields.
One is "rating" which is given a number between 1-5
The other is "flash" with either a 1 or a 0.
What I want to do:
Show all posts with a 1 on flash, in ORDER descending by the "rating"...
I currently have:
$args = array(
'posts_per_page' => 11,
'post_status' => 'publish',
'meta_key' => 'rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'meta_key' => 'flash',
'meta_value' => '1',
)
);
$ultimos = new WP_Query( $args );
This does NOT filter the flash custom field.
however if I do:
$args = array(
'posts_per_page' => 11,
'post_status' => 'publish',
'meta_key' => 'rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'flash',
'meta_value' => '1',
);
$ultimos = new WP_Query( $args );
This DOES filter flash, but does not order them properly.
Any thoughts?
I believe you need to take a look at using the relationship feature of the WP_Query: https://codex.wordpress.org/Class_Reference/WP_Query
$args = array(
'posts_per_page' => 11,
'post_status' => 'publish',
'meta_key' => 'rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'flash',
'value' => '1',
'compare' => 'LIKE',
),
);
$ultimos = new WP_Query( $args );
I'm using utimate member plugin on my website. for each member (author), I have a field to store the member gender. (Homme / Femme).
the data is stored in my database, inside wp_usermeta.
meta key is "gender" and meta value is either "Homme", or "Femme".
I'm trying to write a wp-query to display all posts from all the authors, but only "Homme" authors, and another with only "Femme".
here is my wp-query to display all the posts without filtering by gender :
<?php $custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
);
$custom_query = new WP_Query( $custom_query_args ); ?>
works fine.
Here is what I've tried so far to get only the posts from "Homme" gender, but it's not working... I think I need to add a reference to post author ID somewhere but I can't find the solution.
<?php $custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'gender',
'value' => 'Homme',
'compare' => '='
),
),
'order' => 'DESC',
'orderby' => 'date',
);
$custom_query = new WP_Query( $custom_query_args ); ?>
I don't know if there's a way of doin it with the plugin itself, but i'm pretty sure it can be done with a simple wp-query.
Can anybody help me with this ?
thanks.
Change this:
<?php $custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'gender',
'value' => 'Homme',
'compare' => '='
),
),
'order' => 'DESC',
'orderby' => 'date',
);
For this:
<?php $custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_key' => 'gender',
'meta_value' => 'Homme',
'order' => 'DESC',
'orderby' => 'date',
);
I need to get a list of all post from one category and since some date until today.
The problem is that i dont find a way to ask this to google & im starting using mysql...
I think that it will be something like:
SELECT * FROM `wp_posts` WHERE 1 and the rest...
Use wordpress built in function to get posts
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
If you need to get it from date till now, use class "WP_Query":
$args = array(
'date_query' => array(
array(
'after' => array(
'year' => 2015,
'month' => 1,
'day' => 31,
),
'inclusive' => true,
),
),
'category_name' => 'category',
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
This should give you clue where to continue. For more detailed examples follow Wordpress codex: http://codex.wordpress.org/Class_Reference/WP_Query
So here is my query:
$args = array(
'post_type' => 'Event',
'posts_per_page' => 1000,
'meta_key' => 'event_informations_show_on_the_homepage',
'meta_value' => 'Show on the homepage',
'meta_compare' => '==',
'meta_key' => 'event_informations_date',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$loop = new WP_Query( $args );
I want to select all posts that have the metabox event_informations_show_on_the_homepage and the value of the metabox event_informations_show_on_the_homepage and order by the date metabox which is stored as a timestamp and is called event_informations_date.
What am I doing wrong?
Hopefully I'm not barking up the wrong tree here.
You can use the key 'meta_query' to filter posts by multiple meta keys like so:
$args = array(
'post_type' => 'Event',
'posts_per_page' => 1000,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'event_informations_show_on_the_homepage',
'value' => 'yes',
),
array(
'key' => 'event_informations_date',
'value' => 'yes',
)
)
);
$query = new WP_Query( $args );
What WordPress is doing here is creating multiple wheres against the same column by using innerjoins on the same table, each time using a different alias. It's pretty cool & is probably the fastest way to query like that.
For more information see here: http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
Hope this helps :)