Wordpress: Custom-Field and multiple orderby - wordpress

I have defined several custom post types (CPTs) with some Custom Fields (CF) and need to have a certain sorting - first by category, then by date, something along the lines of
$args = array (
'meta_value category' => 'ASC',
'meta_value startdate' => 'ASC'
),
'post_type' => 'training'
);
$the_query = new WP_Query($args);
However, this does not seem to work and the dates are not correctly parsed, since the default parsing of a meta_value is alphabetical. I cannot set the meta_type to DATE, because there are two meta_values.
Any ideas?

So, I was unable to find a solution. Instead I wrote a custom function in php that took the database result and sorted it to my needs.

Related

Wordpress show all Custom Post Type except one

I am using this loop:
<?php
$search_count = 0;
$search = new WP_Query("s=$s & showposts=-1");
if($search->have_posts()) : while($search->have_posts()) : $search->the_post();
$search_count++;
endwhile; endif;
echo $search_count;
?>
How can I say, that I want to show every Custom Post Type, except for the post type called 'klanten' (clients in Dutch)?
You can use like this:
$args = array(
's' => $s,
'posts_per_page' => -1,
'post_type' => array( 'post', 'page', 'movie', 'book') // include the posts type you want to show
);
$query = new WP_Query( $args );
Unfortunately, there's currently no way you could exclude a specific post_typeby defining it. So, the workaround is you only define the post_type you want to include.
A couple of years too late, but you can use the get_post_types() function with the operator parameter set to not. This way, you will receive an array of all post types except the one(s) you set in the args parameter of the function, which you can then use to define the post_type in your query:
$args = array(
's' => $s,
'posts_per_page' => -1,
'post_type' => get_post_types(array('name' => 'klanten', 'public' => false), 'names', 'not')
);
$query = new WP_Query( $args );
However, you might want to var_dump() the result of the function first, since it will return other hidden post types you might not be interested in (like 'attachments' or others defined by plugins). This is the reason why I added 'public' => false, which will make sure that I only receive public post types. This removes most of the undesired post types, but you should still check ('attachments' is not removed by this).
Another shortcoming of this method is that you can only remove one post type. A workaround for that would be to get all post types and then remove the ones you don't want from the array using the method described here, but that seems like too much code, so in that case you might be better off just setting all the post types you want instead of the ones you don't.

Wordpress filters

I used wordpress to make a website with some custom types. The problem is that i need to implement a filter for these types and I have some questions about the plugins that i'm using for this project:
For to make custom types and views I'm using Types and Views plugins for wordpress.
I made a new custom type with some fields (title, price, some selectors, ...) and I need to filter all the information ordering the results following 2 criterias. I don't know if is it possible to do so using the Views/Types plugin.
I also need to filter the fields of the filter. For example, if i have 2 fields one called Countries and other called Cities, if i select Spain in countries I should see only spanish cities in the second field.
Any idea if i can do that using Types and Views plugins? is there any other plugin?
Thanks in advance!
Any good plugin will save the custom fields as post-meta. You need to find out what the key is for the field you are trying to use as a filter. Once you have that you can use a simple meta query to get the posts you need:
$args = array(
'post_type' => 'post', // The post type
'posts_per_page' => -1, // Get all posts
'orderby' => 'rand', // Random order, also can use 'post_date' or 'post_title'
'order' => 'ASC', // Direction of sort
'meta_key' => 'my_meta_key', // Whatever your key is
'meta_query' => array(
array(
'key' => 'my_meta_key', // Whatever your key is
'value' => 'foo' // What it should be
'compare' => 'IN', // How to compare the value to the stored value
)
)
);
$query = new WP_Query($args); // The query
Here is more on the Wordpress codex.

Change wordpress to display search results in chronological order

This is a Thesis theme site using the core WP search functionality. Not looking for a new plugin solution, just how to alter the search results to change order of posts from relevance to chronological order. Any help is greatly appreciated.
Some of native wp search results, like terms do not have any type time info stored. You can use WP_User_Query with order of records. It will be not exactly what you expected but will have chronological order:
$args = array(
'search' => 'search_patern_from_form',
'orderby' => 'id',
'order' => 'DESC'
);
$user_query = new WP_User_Query( $args );
See order by section of WP_User_Query codex page

WP_Query filter by value of Custom Post Type

I am unable to filter results by a custom post type
This is my while loop which returns all items. I need to filter by a date, only showing date earlier than CURDATE()
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$auction_query = new WP_Query();
$auction_query->query('post_type=auctions&orderby=meta_value&meta_key=auction_details&paged='.$paged.'&posts_per_page=20');
in my wp_postmeta table I have the following data
meta_id: [id of meta row]
post_id: [id of post]
meta_key: auction_details
meta_value: a:6:{s:12:"auction_date";s:10:"2013-07-27";s:12:"auction_time";s:21:"9am Personal Property";s:16:"auction_location";s:44:"New Providence,";s:0:""
I need to filter by the auction date value in the meta value column when paired with the meta_key auction_details showing only posts with auction_date earlier than today's date.
It does not make sense to use CURDATE because the MySQL field type used to store post_meta values is a string, and not a date.
Consider storing your time and date values
in a separate post meta field, and
as a unix timestamp (number value).
Then it will be easy to restrict your query with a > or < comparison, something like
$args = array(
'meta_query' => array(
array(
'key' => 'auction_time',
'value' => date_i18n( 'U' ),
'compare' => '<',
)
)
);
$auction_query = new WP_Query( $args );
If you'd like to order by this field, be sure to order by 'meta_value_num' instead of the usual 'meta_value', as the latter will order your posts lexically and not numerically.
(A word of caution: Be careful to use date_i18n() (a function provided by WordPress using your WordPress timezone preference) rather than date() (a native PHP function using your system wide timezone setting in PHP) when converting timestamps while using WordPress.)

Wordpress search custom fields

I have a custom field with meta_name is product_id. I am trying to determine how to search that field when using the wordpress search.
I know the wordpress search results page uses query_string which contains the "s" value for the keyword searched I just don't know how to change my code below to search the custom field mentioned.
$loop = new WP_Query("$query_string . '&posts_per_page=-1&post_type=product'"); ?>
think this section in the codex should have the answers you need with regards to adding in custom field parameters to you query:
http://codex.wordpress.org/Function_Reference/WP_Query#Custom_Field_Parameters
Not sure whether that will allow you to search within a particular custom field though. Can't be certain but I think you'd have to make some changes on the search form end of things. See how you get on with adding in the custom field parameters.
Off the top of my head I would think you would need something like:
$query = new WP_Query( array(
'meta_key' => 'product_id',
'meta_value' => $query_string,
'posts_per_page' => '-1',
'post_type' => 'product'
) );
Not tested though!

Resources