I want to order by an Advanced Custom Field numeric field, however I can't seem to get it to work:
$args = array(
'posts_per_page' => 20,
'category_name' => 'staff-' . $wp_query->queried_object->post_name,
'meta_key' => 'scroller_order',
'orderby' => 'meta_value',
'type' => 'NUMERIC',
'order' => 'ASC'
);
The ACF field is called scroller_order and it only allows numbers but I want to order by whatever the person puts in that field.
Did you try 'orderby' => 'meta_value_num', instead of simply 'meta_value'
Check out the ACF Documentation too - it's pretty thorough.
Just adding this information because it was removed from the documentation:
query_posts('cat=222&meta_key=YOUR_ACF&orderby=meta_value_num&order=DESC');
Related
I have a meta user of type object on my custom post type.
Now I would like to query all custom-posts which have this meta value. I have not found a way how to query meta values of type object. Is there a way actually ?
$args = array(
'post_type' => 'custom-posts',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'user.id',
'value' => $this->user_id,
),
)
);
Thanks in advance.
When WordPress (or a theme or plugin) stores an object or an array as the value of an item of metadata, it first serializes the object into a text representation.
For example, this object
$obj = array ('first'=>'Jeff', 'last'=>'Atwood',);
when serialized is this text string:
a:2:{s:5:"first";s:4:"Jeff";s:4:"last";s:6:"Atwood";}
So, if you want to search this item of metadata for, let's say, Jef, you'll need something like this:
$args = array(
'post_type' => 'custom-posts',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'user.id',
'value' => 'Jef',
'compare' => 'LIKE'
),
)
);
It will search the text of the serialized object for any row that contains the consecutive letters Jef. In SQL, it uses meta_value LIKE '%Jef%' to look for the string you're matching.
This is acknowledged by everybody involved with it to be a sloppy and slow kind of search. You must check the results it returns to make sure it didn't match the wrong thing. It will match what you do want, but it also may match other things.
I have two custom post types, playlists and games. Playlists have a ACF repeater field, that contain games.
My goal: I am trying to build a simple query, that gets all playlists that DO NOT have this game listed in the repeater field AND alll playlists that are empty (do not have any repeater fields / database entries at all). I want to use the get_posts() function to achieve my goal, if possible.
My problem: I can only get the query to show ONLY the playlists that HAVE the game by using the = operator in the "game_filter" meta query compare field (but I want the opposite). If I choose the != or <> or IS NOT operator, it spits out all available posts. And if I also include my "empty_playlists" meta query, it ALWAYS shows me all playlists, no matter what operators I use, even tho it is a OR and the listed posts do have entrys.
I tried a lot but just cant get it to work. All exmaples I found were about different things, mostly about the % problem with the repeater field names and its solution. I hope someone can help me with this real quick. What am I doing wrong? Please help fellow and better coders! :-)
This is my code
$excluded_playlists_game_id = 274;
$user_id = 1;
$args = array(
'post_type' => PSiCorePostTypes::PLAYLISTS,
'author' => $user_id,
'order_by' => 'title',
'order' => 'ASC',
'suppress_filters' => false,
'meta_query' => array(
'relation' => 'OR',
'game_filter' => array(
'type' => 'NUMERIC',
'key' => 'psi_playlist_games_%_item',
'compare' => '!=',
'value' => $excluded_playlists_game_id,
),
'empty_playlists' => array(
'key' => 'psi_playlist_games_%_item',
'compare' => 'NOT EXISTS',
),
),
);
$user_playlists = get_posts( $args );
and this function for the % problem.
add_filter( 'posts_where', 'get_user_playlists_query_allow_wildcard' );
function get_user_playlists_query_allow_wildcard( $where ) {
global $wpdb;
$where = str_replace(
"meta_key = 'psi_playlist_games_%_item",
"meta_key LIKE 'psi_playlist_games_%_item",
$wpdb->remove_placeholder_escape( $where )
);
return $where;
}
If I were using a SQL statement, this is what it would look like:
select * from courses where active = 1 AND (title = $string OR description = $string)
'courses' is a custom post type
'active' is a custom field created with ACF
'description' is a custom field created with ACF
'title' is the standard WP title for posts
This is what I have so far:
$args_courses = array(
'post_type' => 'courses',
'posts_per_page' => 9999,
'meta_query' => array(
array(
'key' => 'active',
'value' => 1,
'compare' => '='
),
'relation' => 'AND',
array(
'key' => 'description',
'value' => $string,
'compare' => '='
),
'relation' => 'OR',
array(
'key' => 'title',
'value' => $string,
'compare' => '='
)
),
);
It seems as though I can't access the WP title through a meta_query, but can't figure out how to turn my query around so 'active' remains the first column to check on.
Thanks in advance for any help!
What you are trying to do is not supported by the default WP_Query functionality, however you can extend it as described in the following answer from the WordPress Stackexchange site. Note that you you may need to alter it further for your specific use case.
https://wordpress.stackexchange.com/questions/178484/wp-query-args-title-or-meta-value
A few notes on your current query.
relation should only be used once in your meta_query. WordPress uses arrays to do the grouping, and wouldn't know to use "AND" or "OR" in your example.
It looks like you want "all" posts, not really 9999 posts, so you should use -1 to fetch everything in one result set.
I have a custom Post Type called cptMovie which has two Custom Field (Metadata) "Language" and "Box office" assigned to it.
I also have a Custom Taxonomy called movieTax with 4 tax Terms "Action", "Comedy", "Family" ,and "Horror" .
In a WordPress File structe like below:
index.php
page.php
page-movie.php
taxonomy.php
taxonomy-movieTax.php
I am able to list and display all the Custom Post Types (movie) under Taxonomy (movieTax) using indexed page page-movie.php and custom wp-query and eventually link them to the taxonomy-movieTax.php to have all Post under each tax terms.
Up to here I am Ok and getting the resut BUT I need to add some Filters for users like Filtering the result by Language or Boc office Metadata,
Now my question is Which Part of the WP Template Hierarchy is in charge of displaying the result? Do I have to create the search.php if so how a query like this:
$arg = array(
'meta_query' => array(
array(
'key' => 'language',
'value' => 'english'
)
)
);
$filter-lang = new WP_Query( $arg );
in page-movie.php will end up in search.php?
Thanks,
You don't need to create a "search.php" file.
You can add the below code in "page-movie.php" or "taxonomy-movieTax.php" file, as per your requirement.
$arg = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'language',
'value' => $lang_val,
'compare' => '='
),
array(
'key' => 'box_office',
'value' => $box_office_val,
'compare' => '='
)
)
);
$filter-lang = new WP_Query( $arg );
Here, $lang_val & $box_office_val are values to search for language key & box office key respectively. However, if you want to create separate search file, you also need to specify post type in arguments.
I'm trying to select few rows from the table and render it in multiple pages (pagination).
This is a code in the model:
return new CActiveDataProvider('Downloads',
array(
'criteria' => array(
'select' => 'download_id,title,thumb_ext',
'order' => 'download_id DESC',
'limit' => $count,
),
'pagination' => array('pageSize' => 5,),
)
);
In the view I display it using CGridView:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns' => array('download_id', 'title', 'thumb_ext'),
));
The problem is that CActiveDataProvider ignores limit of criteria and returns all rows of table...
Thanks.
I'm not positive... but I think Yii uses the LIMIT clause to do SQL result pagination, so it will overwrite/replace your LIMIT clause. You can check this by turning on the CWebLogRoute log route to see exactly what SQL is being executed.
I'm not quite sure how this is supposed to work, anyway. What is the LIMIT clause you added for? If you are paginating anyway, why not let the user paginate through all the records? The solution is probably to change your criteria to get rid of the LIMIT clause.
Are you trying to set the number of results per page? You already have the pageSize set to 5 though...
One other thing to try that might do what you want, is to check out the base Pager class, CPagination. With CLinkPager, it might let you paginate more creatively than the CListPager you are using with the CGridView.
Good luck!
I had the same problem and I found a solution as follows:
return new CActiveDataProvider('Downloads',
array(
'criteria' => array(
'select' => 'download_id,title,thumb_ext',
'order' => 'download_id DESC',
),
'pagination' => array('pageSize' => 5,),
'totalItemCount' => $count,
)
);
and set CGridView to pagination is hidden:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'enablePagination' => false,
'columns' => array('download_id', 'title', 'thumb_ext'),
));
One of the basic features of a data provider is that you can override the calculation of the amount of rows by specifying a "totalItemCount" in the config. Normally (I haven't tested it) this also works for the ActiveDataProvider:
return new CActiveDataProvider('Downloads',
array(
'criteria' => array(
'select' => 'download_id,title,thumb_ext',
'order' => 'download_id DESC',
),
'pagination' => array('pageSize' => 5,),
'totalItemCount' => $count,
)
);