I'm struggling to make two custom queries for visual composer:
a query which will display posts which are published with a date after today's date (have plugin which publishes future posts) in Ascending or descending order (whichever makes it show the soonest upcoming event first)
a query which will display only posts which meet multiple category requirements (eg. categories 'upcoming' and 'class' or 'upcoming' and 'social'
Really, either of these should give me the results I want, but the first would be most convenient.
I can't for the life of me decipher the Codex's page on custom queries and figure out how to get them to work outside of the context of a php page, and in the context of the visual compose.
One thought I had is that perhaps I need to add a custom query to my functions.php that has a name, and then call the query with the variables from Visual Composer? Is that the right idea? How would I do such a thing/call the new query?
I've tried the following to satisfy #1 and it seems to have disregarded the query altogether and just displayed all the events:
[vc_basic_grid post_type="custom" grid_id="vc_gid:1473741223498-3776c0d3-292b-4" custom_query="WP_Query( ''showposts=20&monthnum=' . date_query( array( 'after', . NOW() . ) ) . '&order_by=date&order=DESC')"]
[vc_separator]
[vc_basic_grid post_type="custom" grid_id="vc_gid:1473741223504-67e7758b-8892-6" custom_query="$args = array(
'date_query' => array(
array(
'after' => NOW(),
'inclusive' => true,
),
),
'posts_per_page' => -1, 'order_by' => 'date', 'order' => 'ASC'
);
$query = new WP_Query( $args );"]
Would love some nudges in the right direction!
Thank you :)
I did something similar not too long ago and used the following as far as the date & making sure it only pulled posts from "today" and in the future.
_EventStartDate=>$today
That was for pulling upcoming events for a custom post type. With that said, this code is for your scenario which is retrieving normal posts:
date=>$today
Here's my whole custom query that works to pull events but I'm stuck on trying to figure out how to pull from multiple categories as well:
post_type=tribe_events&tribe_events_cat=featured&post_status=publish&_EventStartDate=>$today&orderby=_EventStartDate&order=asc
Please follow up once you have a solution.
Related
I have a custom post type called BID which contains a user's bids (similar to freelancer.com, upwork, etc) However I need the BID results on the page to be ordered by meta stored in a users profile, which is a custom post type PROFILE.
How would I go about doing this?
Here is my current Query
WP_Query( array( 'post_type' => array(BID, PROFILE),
'post_parent' => get_the_ID(),
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'rating_score',
'post_status' => array('publish','complete', 'accept', 'unaccept')
)
);
I don't know of a way to do this with WP_Query alone so I'd probably write some extra PHP to do this. I'm not sure exactly how things are set up so I can't write code, but here's the general idea.
Generate your bid results without paying much attention to how they are ordered.
Loop through your generated query result and, for each item, define a new attribute that fetches the meta information from the corresponding profile meta.
Order your array using this new meta information. You might be able to use PHP's sort() function, or if you need a more general function, try usort()
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.
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.
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
my question has to do with the pods framework plugin for wordpress sites. I am using pods version 2.2 and have been having trouble with the where parameter in the find() function.
I'd be suprised if I am the first person to encounter this problem but I have searched extensively and haven't found anyone providing an answer (or question at that).
Anyway, I'll give an example to highlight my problem.
Say I have a Bands Pod and a Records Pod and these two pods have a bi-directional multi-select relationship between them (that is, an n to n relationship). Hence a band can have numerous records and a record can have multiple bands. Further, the relationship exists between the fields BandsPod('records') and RecordsPod('bands').
Now, I can retrieve all records in the records pod like so (note the where is commented out):
$pods = pods('records');
$params = array(
'select' => 't.*',
'limit' => -1
//,'where' => '???'
);
$pods->find($params);
Then do whatever I want with them, e.g. template(), fetch(), etc.
My trouble occurs when I want to filter by band. What should my where statement be if I want to retrieve all records by band with id 1?
My view is that it should be something like this:
$params = array(
'select' => 't.*',
'limit' => -1,
'where' => '1 IN bands.id'
);
$pods->find($params);
However, this does not work (not that I especially expected it to).
It would also be desirable to know how this would work for filtering by multiple bands. E.g.
'where' => '(1, 2) IN bands.id'
As I said before, I've been trying to get this to work for some time and with little joy. I have managed to get it working but in a very ugly way. I.e.
Get bands from Bands Pod with relevant band ids,
Collect all ids of records from the bands pod records field,
Write params for Records Pod
Use the ids in the where statement to check they match with t.id,
$pods->find(); //where $pods=pods('records');
Thanks in advance for taking the time to read this and any answers you may give.
Cheers,
Joe
N.B. I know about the filters() function and it does not do what I'm after. I'd like stuff specifically for the where statement please.
You would want:
'where' => 'bands.id IN ( 1, 2 )'
If your bands are a custom post type, it would be:
'where' => 'bands.ID IN ( 1, 2 )'