I have a "custom Page" set up and I am trying to figure out how I can make it arrange the posts it pulls in a specific order...I am thinking of using the custom fields. But not sure how to add a listing of a numeric value.
For Lease and For Sale
List item Size List item (ie 1 br, 2br, 3br) (least expensive
first)
List item Price (Cheapest to most expensive)
You can order order posts by custom fields. The query will be something like this:
$query = new WP_Query( array ( 'post_type' => 'custom_page', 'order' => 'DESC', 'orderby' => 'meta_value', 'meta_key' => 'price' ) );
In your case, you will have to convert price and size to pure numeric values for the sorting to work. For Lease and Sale, I would suggest you should not rely on sorting but use it to query, something like this:
$query = new WP_Query( array( 'post_type' => 'custom_page', 'meta_key' => 'deed', 'meta_value' => 'lease' ) );
Related
How do I get IDs of all products in one array?
The output should be a simple array containing some numbers, nothing more.
I have been advised against using query_posts(), so I would prefer a solution not using this function.
There is multiple ways to get all product ids in an array:
1) Using Woocommerce WC_Product_Query:
$ids = wc_get_products( array( 'return' => 'ids', 'limit' => -1 ) );
2) Using Wordpress WP_Query (including product variations IDs),
$ids = get_posts( array(
'posts_per_page' => -1,
'post_type' => array('product','product_variation'),
'fields' => 'ids',
) );
3) Using a WPDB (a SQL query) (including product variation IDS):
global $wpdb;
$ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type IN ('product','product_variation')");
In the 2 last ways you can remove 'product_variation' post type, if you don't want it.
I have two custom post types, Clinics and Clinic Promos. I am having one issue as to how to add a second query. Once I query clinics by list of zipcodes, I want to filter those down by running a new query or possibly joining the queries by querying the Post ID of the clinic that is saved under the clinic promos, which is how I relate the two.
/*More PHP before this line determines list of zip codes*/
$zipcodelist = implode(', ', $variabele);
mysqli_close($dbc);
$args = array(
'orderby' => 'post_title',
'order' => 'DESC',
'post_type' => 'clinics',
'meta_query' => array (
array (
'key' => 'cliniczipcode',
'value' => $zipcodelist,
'compare' => 'IN'
)
) );
// the query
$the_query = new WP_Query( $args );
So now that I have the list of clinics, now I need to take their ID's of the clinics and do a query for Clinic Promos post type and filter them down to only the clinic promos with those ID's.
Does anyone have any direction I can take? Any advice on how I could loop these or join them?
Scenario: I am using WordPress + Woocommerce on my site. On my product category page, I want to add another search filter other than the defaults offered by WC. This filter would show the products that are going to end soon.
My approach to this part is I set the expiration date of the product and then write a query that would sort the products by remaining number of days from the expiration date.
I have done most of the part (thanks to Google). Here is the query what I have written so far.
<?php
$event_query = new WP_Query(
array(
'post_type' => 'product',
'meta_key' => '_crowdfundingtodatepicker',
'order_by' => 'meta_value',
'order' => 'asc',
'meta_query' => array(
array(
'key' => '_crowdfundingtodatepicker',
'value' => date("m/d/Y"),
'compare' => '>',
'type' => 'CHAR'
)
)
)
);
?>
The format of _crowdfundingtodatepicker in the database is "m/d/Y" i.e "10/30/2015".
I am stuck at the compare part. The product going to end soonest would be the first one on the display and so on. i.e.
The product with expiration date of "11/01/2015" would be shown first than the product with expiration date of "12/01/2015". Any help?
i have my wordpress posts with two custom fields
'episode_number'
'season_number'
i want to select all posts with season_number=5(example) , and sort them by episode_number DESC
i've written this
$max_season=5;
$args = array(
'meta_key' => 'season_number',
'meta_compare' =>'==',
'meta_value' => $max_season,
'orderby' => 'episode_number',
'order' => 'DESC',
);
$wp_query = new WP_Query($args);
the selection based on season_number works , but i can't order them , with this code wordpress order them based on the time of publishing
anyone can help me?
I believe WordPress will only query the specified metadata you requested, which is key=season_number, value=5 (the season number). It will not query the post metadata for the key episode_number, so there is no way you can sort by that field directly in the SQL query.
You either need to loop the result of the query and get each item's metadata for key=episode_number and populate a new sorted array accordingly or figure out a way to query also this metadata in a single query as well.
For the second one, I have no idea how this will work.
My original answer was off the mark, I misread the question
Try something like this:
$max_season=5;
$args = array(
'meta_key' => 'episode_number',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'season_number',
'value' => array($max_season),
'compare' => 'IN',
)
)
);
$wp_query = new WP_Query($args);
I am having a little problem. I am trying to get the latest 12 post by a custom meta field. Out of those 12 posts, I want to order them by post date. So first I pull 12 posts out using a custom meta field and ordering them to find the latest through the meta field. Once I have them, then I want to re-order them with latest post.
Here is my current code, I don't know how I can put two order-bys in one query...
$recentEpisodes12 = new WP_Query(array(
'posts_per_page' => 12,
'post_type' => 'post',
'meta_key' => 'air_date',
'order' => 'DESC',
'orderby' => 'meta_value_num',
'meta_query' => array(
array(
'key' => 'air_date',
),
array(
'key' => 'already_aired',
'value' => 'yes',
'compare' => '='
)
),
));
In WordPress 4.2 and up, ordering by one or more custom fields was made much easier. See this link for examples: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/
You can even order one column ASC and another DESC by passing an array to orderby now:
'orderby' => array(
'city_clause' => 'ASC',
'state_clause' => 'DESC',
),
According to the Codex, you simply need to separate them by a space:
Multiple 'orderby' values
Display pages ordered by 'title' and 'menu_order'. (title is
dominant):
$query = new WP_Query( array( 'post_type' => 'page', 'orderby' => 'title menu_order', 'order' => 'ASC' ) );
In your case, that would look like:
'orderby' => 'meta_value_num date'
EDIT: Okay, it seems like you're trying to do something a bit more complex here. This is how I interpret it, correct my if I'm wrong:
Order by air_date (in descending order, newest first).
Keep only the 12 newest items according to air_date.
Order the resulting 12 items by date.
What orderby does is basically:
Order by air_date.
If any items have identical air_date values, order those by date.
Keep only the top 12 items.
The difference is that you only want to distinguish by air_date, whereas the normal usage of orderby uses both criteria to determine which items end up in the result.
I don't know an easy way to solve this, though. However, since you only want to change the display order of the resulting items, you could just sort those yourself. You can use get_posts instead of WP_Query and sort the results array using PHP's usort
$posts = get_posts(...);
usort($posts, '__sort_by_date_desc');
function __sort_by_date_desc($a, $b) {
// Make timestamps from MySQL datetime values
$a_date = mysql2date('U', $a->post_date);
$b_date = mysql2date('U', $b->post_date);
// Descending order, swap these for ascending
return $b_date - $a_date;
}
So if you want to order them by post date, why do you need a meta field for that? Once you get the 12 latest posts by meta value, is the air_date somehow different from the post date?
Worth noting: posts_per_page doesn't limit the total number of returns. It just tells WordPress when to split into a new page.
Something like this should work based on what you described in your original post.
// Post Criteria
$post_criteria = array(
'posts_per_page' => 12, // Don't paginate until after 12 posts
'orderby' => 'post_date', // Order by post date
'order' => 'DESC', // Order them reverse chronologically (ie. get the newest first)
'meta_key' => 'air_date', // Only get posts with this meta key
'meta_value' => 'meta_value_num', // And this meta value (only needed if you have multiple possible values for the meta key and just want one)
'post_type' => 'post', // Only get posts
'post_status' => 'publish'); // Only get posts that are published (ie. no drafts, etc.)
// Get posts matching criteria
$posts = get_posts( $post_criteria );
// Discard posts after the 12th one
$posts = array_slice($posts, 0, 12);
// If you wanted to reverse them to display
// chronologically, uncomment this variable.
# $posts = array_reverse($posts);
// For each post
foreach ($posts as $post) {
// Basic variables
// See a full list here: http://codex.wordpress.org/Function_Reference/get_post#Return
$id = $post->ID;
$title = $post->post_title;
$url = get_permalink($id);
$content = $post->post_content;
// Do stuff with the posts here.
}