Query page templates on Wordpress - php

I have a theme in Wordpress which use a code to show products of Woocommerce. However, I want to use the same function for showing also pages with a certain template. Here is what I have done until now:
<div>
<?php
$args = array(
'post_type' => 'page',
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => 'room-template.php'
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div>
<div>
<?php if(has_post_thumbnail()) { the_post_thumbnail(); }?>
<div>
<div>
</div>
</div>
</div>
<div>Buy Now</div>
</div>
<?php endwhile;
} else {
echo __( 'No products found');
}
wp_reset_postdata();
?>
</div>
Whereas, I have 2 pages with this template, the result is "No products found". P.S. The divs are there because I use them for CSS classes. I've just removed the classed to make it easier to read.
Here is the output of the WP_Query:
WP_Query Object ( [query] => Array ( [post_type] => page [post_status] => publish [meta_query] => Array ( [0] => Array ( [key] => _wp_page_template [value] => room-template.php )))[query_vars] => Array ( [post_type] => page [post_status] => publish [meta_query] => Array ( [0] => Array ( [key] => _wp_page_template [value] => room-template.php ))[error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [paged] => 0 [comments_popup] => [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [fields] => [menu_order] => [category__in] => Array ( )[category__not_in] => Array ( )[category__and] => Array ( )[post__in] => Array ( )[post__not_in] => Array ( )[tag__in] => Array ( )[tag__not_in] => Array ( )[tag__and] => Array ( )[tag_slug__in] => Array ( )[tag_slug__and] => Array ( )[post_parent__in] => Array ( )[post_parent__not_in] => Array ( )[author__in] => Array ( )[author__not_in] => Array ( )[suppress_filters] => [ignore_sticky_posts] => [cache_results] => 1 [update_post_term_cache] => 1 [update_post_meta_cache] => 1 [posts_per_page] => 10 [nopaging] => [comments_per_page] => 50 [no_found_rows] => [order] => DESC )[tax_query] => WP_Tax_Query Object ( [queries] => Array ( )[relation] => AND )[meta_query] => WP_Meta_Query Object ( [queries] => Array ( [0] => Array ( [key] => _wp_page_template [value] => room-template.php ))[relation] => AND )[date_query] => [request] => SELECT SQL_CALC_FOUND_ROWS exitn_posts.ID FROM exitn_posts INNER JOIN exitn_postmeta ON (exitn_posts.ID = exitn_postmeta.post_id) WHERE 1=1 AND exitn_posts.post_type = 'page' AND ((exitn_posts.post_status = 'publish')) AND ( (exitn_postmeta.meta_key = '_wp_page_template' AND CAST(exitn_postmeta.meta_value AS CHAR) = 'room-template.php') ) GROUP BY exitn_posts.ID ORDER BY exitn_posts.post_date DESC LIMIT 0, 10 [posts] => Array ( )[post_count] => 0 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 0 [max_num_pages] => 0 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => [is_date] => [is_year] => [is_month] => [is_day] => [is_time] => [is_author] => [is_category] => [is_tag] => [is_tax] => [is_search] => [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => 1 [is_404] => [is_comments_popup] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => [query_vars_hash:WP_Query:private] => 436de9012210c4e9c717fd5edf527108 [query_vars_changed:WP_Query:private] => 1 [thumbnails_cached] => [stopwords:WP_Query:private] => )

You want to use get_template_part to print a specific template.
https://developer.wordpress.org/reference/functions/get_template_part/
If you want to use a specific function() within a template file then you might want to move this function to a common function library or your functions.php file.
-- EDIT --
SELECT SQL_CALC_FOUND_ROWS exitn_posts.ID
FROM exitn_posts INNER JOIN exitn_postmeta ON (exitn_posts.ID = exitn_postmeta.post_id)
WHERE 1=1 AND exitn_posts.post_type = 'page'
AND ((exitn_posts.post_status = 'publish'))
AND ( (exitn_postmeta.meta_key = '_wp_page_template' AND CAST(exitn_postmeta.meta_value AS CHAR) = 'room-template.php') )
GROUP BY exitn_posts.ID ORDER BY exitn_posts.post_date DESC LIMIT 0, 10
If executing this on the Database doesn't bring up the data it means either it doesn't exist or something within is missing. Its hard to tell this from my hand. I have pretty much the same pattern in one of my personal query and its Data is returned without any issues.
Example :
$args = array(
'post_type' => explode( ',', $post_type ),
'tag' => $tag,
'p' => $id,
'category_name' => $category,
'posts_per_page' => $posts_per_page,
'order' => $order,
'orderby' => 'meta_value',
'meta_key' => '_my_named_key',
'meta_query' => array(
array(
'key' => '_my_named_key'
)
),
'no_found_rows' => 1
);
-- Working Edit --
Make sure the value is a existing in the Database by looking it via something like phpMyAdmin or making a SQL Query onto it. This case was solved in chat full value had an unknown prefix page-templates/.

Related

Cant understand the logic to nest multiple times the array

I have a problem with my logic in the function.I'm trying to nest array X times so that it can look like a tree with children.I nested it with the first child but when i go deep i cant find solution to remove them from the Main array and add them deep.The "parent" key is the ID of the parent element that needs to have child.Any help appreciate.
function array_search_multidim($array, $column, $key){
return (array_search($key, array_column($array, $column)));
}
public function get_all() {
$full_menu = $this->Site_model->get_all_menu();
usort($full_menu, function($a, $b){
return strcmp($a->menu_order, $b->menu_order);
});
foreach($full_menu as $fm) {
$mega_menu[] = array(
'id' => $fm->id,
'text' => $fm->title,
'href' => $fm->link,
'icon' => $fm->icon,
'target' => $fm->target,
'title' => $fm->name,
'order' => $fm->menu_order,
'parent' => $fm->parent
);
if($fm->parent != 0) {
$child_menu[] = array(
'id' => $fm->id,
'text' => $fm->title,
'href' => $fm->link,
'icon' => $fm->icon,
'target' => $fm->target,
'title' => $fm->name,
'order' => $fm->menu_order,
'parent' => $fm->parent
);
}
}
foreach($child_menu as $cm) {
$mega_menu[$this->array_search_multidim($mega_menu,'id',$cm['parent'])]['children'][] = array(
'id' => $cm['id'],
'text' => $cm['text'],
'href' => $cm['href'],
'icon' => $cm['icon'],
'target' => $cm['target'],
'title' => $cm['title'],
'order' => $cm['order'],
'parent' => $cm['parent']
);
}
echo '<pre>';
print_r($mega_menu);
echo '</pre>';
}
For now i recieve array like that
Array
(
[0] => Array
(
[id] => 1
[text] => Начало
[href] => http://localhost/roni/#top
[icon] => fas fa-home
[target] => _self
[title] => Начало
[order] => 1
[parent] => 0
)
[1] => Array
(
[id] => 4
[text] => Споделен хостинг
[href] => http://localhost/roni/#shared
[icon] => fas fa-home
[target] => _blank
[title] => shared
[order] => 1
[parent] => 3
[children] => Array
(
[0] => Array
(
[id] => 5
[text] => Софтуер
[href] => http://localhost/roni/#software
[icon] => fas fa-code
[target] => _self
[title] => software
[order] => 2
[parent] => 4
)
)
)
[2] => Array
(
[id] => 2
[text] => Интернет
[href] => http://localhost/roni/#internet2
[icon] => fas fa-wifi
[target] => _top
[title] => Интернет
[order] => 2
[parent] => 0
)
[3] => Array
(
[id] => 5
[text] => Софтуер
[href] => http://localhost/roni/#software
[icon] => fas fa-code
[target] => _self
[title] => software
[order] => 2
[parent] => 4
)
[4] => Array
(
[id] => 3
[text] => Хостинг
[href] => http://localhost/roni/#hosting
[icon] => fas fa-home
[target] => _self
[title] => hosting
[order] => 3
[parent] => 0
[children] => Array
(
[0] => Array
(
[id] => 4
[text] => Споделен хостинг
[href] => http://localhost/roni/#shared
[icon] => fas fa-home
[target] => _blank
[title] => shared
[order] => 1
[parent] => 3
)
)
)
[5] => Array
(
[id] => 6
[text] => Сервиз
[href] => http://localhost/roni/#service
[icon] => fas fa-wrench
[target] => _self
[title] => service
[order] => 5
[parent] => 0
)
[6] => Array
(
[id] => 7
[text] => Контакти
[href] => http://localhost/#contacts
[icon] => fas fa-address-book
[target] => _self
[title] => contacts
[order] => 6
[parent] => 0
)
)
I've kept the initial data as objects because I wanted to use array_walk_recursive() to find the point in which I want to add the nodes. But the basic logic is to look for any nodes that have a parent node, then search all of the leaf nodes to find if that is the node which is the parent. If it is then just add the node...
usort($full_menu, function($a, $b){
return strcmp($a->menu_order, $b->menu_order);
});
foreach ($full_menu as $key=>$menu ) {
if ( $menu->parent != 0 ) {
array_walk_recursive($full_menu, function (&$data) use ($menu) {
if ( $data->id == $menu->parent) {
$data->children[] = $menu;
}
});
}
}
function translate ( &$menu ) {
foreach ( $menu as &$menuItem ) {
$out = array(
'id' => $menuItem->id,
'text' => $menuItem->title,
'href' => $menuItem->link,
'icon' => $menuItem->icon,
'target' => $menuItem->target,
'title' => $menuItem->name,
'order' => $menuItem->menu_order,
'parent' => $menuItem->parent
);
if ( isset($menuItem->children)) {
$out['children'] = $menuItem->children;
translate($out['children']);
}
$menuItem = $out;
}
unset ( $menuItem );
}
translate ($full_menu);
$full_menu = array_filter ( $full_menu, function ($data ) { return $data['parent'] == 0;});
print_r($full_menu);
The last parts I've added will reformat the elements to be the array format you wanted and then it filters out the root level menu to remove any items that have been moved.
In the first foreach in get_all function you add element of $full_menu arrach to $mega_menu every time, event if $fm->parent != 0 (which means every child element is saved to the 1st level of array). Next you handle childs. You must save (at the 1st dimention on array) only those elements with $fm->parent == 0. You can do it by changing a condition in get_all.
function array_search_multidim($array, $column, $key){
return (array_search($key, array_column($array, $column)));
}
public function get_all() {
$full_menu = $this->Site_model->get_all_menu();
usort($full_menu, function($a, $b){
return strcmp($a->menu_order, $b->menu_order);
});
foreach($full_menu as $fm) {
if($fm->parent == 0) {
$mega_menu[] = array(
'id' => $fm->id,
'text' => $fm->title,
'href' => $fm->link,
'icon' => $fm->icon,
'target' => $fm->target,
'title' => $fm->name,
'order' => $fm->menu_order,
'parent' => $fm->parent
);
} else {
$child_menu[] = array(
'id' => $fm->id,
'text' => $fm->title,
'href' => $fm->link,
'icon' => $fm->icon,
'target' => $fm->target,
'title' => $fm->name,
'order' => $fm->menu_order,
'parent' => $fm->parent
);
}
}
foreach($child_menu as $cm) {
$mega_menu[$this->array_search_multidim($mega_menu,'id',$cm['parent'])]['children'][] = array(
'id' => $cm['id'],
'text' => $cm['text'],
'href' => $cm['href'],
'icon' => $cm['icon'],
'target' => $cm['target'],
'title' => $cm['title'],
'order' => $cm['order'],
'parent' => $cm['parent']
);
}
echo '<pre>';
print_r($mega_menu);
echo '</pre>';
}
If you want to remove an element from an array you can use unset.
For example
unset($mega_menu[4][0]);

How to get the highest and lowest Values and sum from Multidimensional array in PHP

I have tried using this example here Return index of highest value in an array
But that does not go into a multi dimensional Array
I have tried ARRAY_COLUMN(), array_values(), array_shift, Max and Min but they are not getting the info i need
I want to be able to loop thru and get to:
[pricing]
then Check pricing to see which is the highest and lowest in the nested array
so like below which total was the Highest and lowest trades on these dates
[2017-09-22]
[2017-09-23]
obviously [2017-09-23] is a simple one, i just dint want to add much more code for ppl helping to go thru.
The Array i create looks like this:
Array
(
[2017-09-23] => Array
(
[0] => Array
(
[timestamp] => 1506169387000
[pricing] => 9.5470
[qty] => 25
[total] => 238.675
[date] => 2017-09-23
)
)
[2017-09-22] => Array
(
[0] => Array
(
[timestamp] => 1506093083000
[pricing] => 9.6300
[qty] => 25
[total] => 240.75
[date] => 2017-09-22
)
[1] => Array
(
[timestamp] => 1506077220000
[pricing] => 8.7190
[qty] => 13
[total] => 113.347
[date] => 2017-09-22
)
[2] => Array
(
[timestamp] => 1506077109000
[pricing] => 8.6800
[qty] => 83
[total] => 720.44
[date] => 2017-09-22
)
[3] => Array
(
[timestamp] => 1506065258000
[pricing] => 8.7100
[qty] => 25
[total] => 217.75
[date] => 2017-09-22
)
)
in the example above i would like it to Create a new array with only the following
date -> last timestamp -> Highest pricing -> Lowest lowest -> Total of all Totals -> first Timestamp
EDIT: the last and first Timestamp are basically the first and last index so in this case:
[timestamp] => 1506093083000 and [timestamp] => 1506065258000
or Index [0] and index [3]
The array from your question:
$array = array (
'2017-09-23' =>
array (
0 =>
array (
'timestamp' => '1506169387000',
'pricing' => '9.5470',
'qty' => '25',
'total' => '238.675',
'date' => '2017-09-23',
),
),
'2017-09-22' =>
array (
0 =>
array (
'timestamp' => '1506093083000',
'pricing' => '9.6300',
'qty' => '25',
'total' => '240.75',
'date' => '2017-09-22',
),
1 =>
array (
'timestamp' => '1506077220000',
'pricing' => '8.7190',
'qty' => '13',
'total' => '113.347',
'date' => '2017-09-22',
),
2 =>
array (
'timestamp' => '1506077109000',
'pricing' => '8.6800',
'qty' => '83',
'total' => '720.44',
'date' => '2017-09-22',
),
3 =>
array (
'timestamp' => '1506065258000',
'pricing' => '8.7100',
'qty' => '25',
'total' => '217.75',
'date' => '2017-09-22',
),
),
);
To get the values maybe you can use array_map and with this the function array_column is the key of all, try with this:
$values = array_map(function($dates) {
$timestamps = array_column($dates, 'timestamp');
$pricings = array_column($dates, 'pricing');
return [
'max_pricing' => max($pricings),
'lowest_pricing' => min($pricings),
'total_of_totals' => array_sum(array_column($dates, 'total')),
'first_timestamp' => reset($timestamps),
'last_timestamp' => end($timestamps),
];
}, $array);
$values is an array with the filter values that you need:
Array
(
[2017-09-23] => Array
(
[max_pricing] => 9.5470
[lowest_pricing] => 9.5470
[total_of_totals] => 238.675
[first_timestamp] => 1506169387000
[last_timestamp] => 1506169387000
)
[2017-09-22] => Array
(
[max_pricing] => 9.6300
[lowest_pricing] => 8.6800
[total_of_totals] => 1292.287
[first_timestamp] => 1506093083000
[last_timestamp] => 1506065258000
)
)
EDIT
Get the pricing of the [first_timestamp] and [last_timestamp]
$values = array_map(function($dates) {
$timestamps = array_column($dates, 'timestamp');
$pricings = array_column($dates, 'pricing');
return [
'max_pricing' => max($pricings),
'lowest_pricing' => min($pricings),
'total_of_totals' => array_sum(array_column($dates, 'total')),
'first_timestamp' => [
'value' => reset($timestamps),
'pricing' => $pricings[each($timestamps)['key']]
],
'last_timestamp' => [
'value' => end($timestamps),
'pricing' => $pricings[each($timestamps)['key']]
]
];
}, $array);
I added an array to both timestamps with the value of these and the pricing.
Array
(
[2017-09-23] => Array
(
[max_pricing] => 9.5470
[lowest_pricing] => 9.5470
[total_of_totals] => 238.675
[first_timestamp] => Array
(
[value] => 1506169387000
[pricing] => 9.5470
)
[last_timestamp] => Array
(
[value] => 1506169387000
[pricing] => 9.5470
)
)
[2017-09-22] => Array
(
[max_pricing] => 9.6300
[lowest_pricing] => 8.6800
[total_of_totals] => 1292.287
[first_timestamp] => Array
(
[value] => 1506093083000
[pricing] => 9.6300
)
[last_timestamp] => Array
(
[value] => 1506065258000
[pricing] => 8.7100
)
)
)

Extracting values from JSON in PHP

I am currently experimenting with the Discogs API, using PHP. My query against their API is returned as JSON, which I can decoding using:
$trackMeta = json_decode($trackMeta, true);
I'd then like to be able to access certain elements within the data that is returned. Using print_r($trackMeta);outputs the below data:
Array ( [pagination] => Array ( [per_page] => 1 [pages] => 7 [page] => 1 [urls] => Array ( [last] => http://api.discogs.com/database/search?artist=siren&q=snorkel&per_page=1&page=7 [next] => http://api.discogs.com/database/search?artist=siren&q=snorkel&per_page=1&page=2 ) [items] => 7 ) [results] => Array ( [0] => Array ( [style] => Array ( [0] => Drum n Bass ) [thumb] => http://api-img.discogs.com/cf1HxM29IXQKNsSdrwYioa0uEeI=/fit-in/150x150/filters:strip_icc():format(jpeg):mode_rgb()/discogs-images/R-3581480-1336678182-5777.jpeg.jpg [format] => Array ( [0] => Vinyl [1] => 12" [2] => 45 RPM ) [country] => UK [barcode] => Array ( ) [uri] => /Siren-21-Vicious-Circle-Snorkel-SPY-Remix-Solitude/master/433189 [community] => Array ( [want] => 66 [have] => 81 ) [label] => Array ( [0] => Siren Records ) [catno] => SIREN001 [year] => 2012 [genre] => Array ( [0] => Electronic ) [title] => Siren (21) / Vicious Circle (3) - Snorkel (S.P.Y. Remix) / Solitude [resource_url] => http://api.discogs.com/masters/433189 [type] => master [id] => 433189 ) ) )
When trying to use a foreach loop on $trackMeta, I can only return 171 which I believe relates to per_page pages and page.
How can I access data deeper in this array? For example values such as thumb or year or genre?
$trackMeta is an associative array:
$trackMeta = array (
'pagination' => array (
'per_page' => 1,
'pages' => 7,
'page' => 1,
'urls' => array (
'last' => 'http://api.discogs.com/database/search?artist=siren&q=snorkel&per_page=1&page=7',
'next' => 'http://api.discogs.com/database/search?artist=siren&q=snorkel&per_page=1&page=2'
),
'items' => 7
),
'results' => array (
0 => array (
'style' => array (
0 => 'Drum n Bass'
),
'thumb' => 'http://api-img.discogs.com/cf1HxM29IXQKNsSdrwYioa0uEeI=/fit-in/150x150/filters:strip_icc():format(jpeg):mode_rgb()/discogs-images/R-3581480-1336678182-5777.jpeg.jpg',
'format' => array (
0 => 'Vinyl',
1 => '12"',
2 => '45 RPM'
),
'country' => 'UK',
'barcode' => array ( ),
'uri' => '/Siren-21-Vicious-Circle-Snorkel-SPY-Remix-Solitude/master/433189',
'community' => array (
'want' => 66,
'have' => 81
),
'label' => array (
0 => 'Siren Records'
),
'catno' => 'SIREN001',
'year' => 2012,
'genre' => array (
0 => 'Electronic'
),
'title' => 'Siren (21) / Vicious Circle (3) - Snorkel (S.P.Y. Remix) / Solitude',
'resource_url' => 'http://api.discogs.com/masters/433189',
'type' => 'master',
'id' => 433189
)
)
);
For example, you can access thumb with the following code:
print_r($trackMeta['results'][0]['thumb']);

Using CArrayDataProvider to populate a CGridView in Yii

CArrayDataProvider Object
(
[keyField] =>
[rawData] => Array
(
[24] => Array
(
[ids] => 24
[name] => trillia
)
[25] => Array
(
[ids] => 25
[name] => ahestina
)
[28] => Array
(
[ids] => 28
[name] => piyas
)
[59] => Array
(
[ids] => 59
[name] => sitesrs
)
[60] => Array
(
[ids] => 60
[name] => simons
)
[70] => Array
(
[ids] => 70
[name] => Mahesh Raj
)
[71] => Array
(
[ids] => 71
[name] => kathetrine
)
[72] => Array
(
[ids] => 72
[name] => babig
)
[73] => Array
(
[ids] => 73
[name] => symons
)
)
[caseSensitiveSort] => 1
[_id:CDataProvider:private] =>
[_data:CDataProvider:private] =>
[_keys:CDataProvider:private] =>
[_totalItemCount:CDataProvider:private] =>
[_sort:CDataProvider:private] =>
[_pagination:CDataProvider:private] => CPagination Object
(
[pageVar] => page
[route] =>
[params] =>
[validateCurrentPage] => 1
[_pageSize:CPagination:private] => 10
[_itemCount:CPagination:private] => 0
[_currentPage:CPagination:private] =>
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
from the above array i populate the cgridview , using the following code in view file
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'unen-custom-module-grid',
'dataProvider'=>$unenrollProvider,
//'dataProvider'=>Yii::app()->globals->getUsersByStandardnSection(1,3),
'itemsCssClass'=>'table table-striped flip-scroll',
'ajaxUpdate'=>true,
'template'=>'{summary}{pager}{items}{summary}{pager}',
'pager'=>array(
'header' => '',
'firstPageLabel' => '<<',
'prevPageLabel' => 'PREVIOUS',
'nextPageLabel' => 'NEXT',
'lastPageLabel' => '>>',
),
// 'selectableRows' => 2,
'columns'=>array(
array(
'header'=>'#',
'value'=>'++$row',
'htmlOptions'=>array(
'style'=>'width:50px;'
)
),
'name',
array(
'id' => 'unselectedIds',
// 'name' => 'ids',
'selectableRows' => 2,
'class' => 'CCheckBoxColumn',
//'value'=>'$this->ids'
),
),
));
?>
its working fine , but whenever i click the pagination the cgridview is automatically hiding , im not using $model->search() here ,
every time cgridview pagination sends get requests only, with out form post data you cant able to populate the cgrid again and again , so try to store the values in session and access it , i hope its usefull to you , try it
CGridView requires the result returned by CActiveDataProvider so try to do this in your search method in the corresponding model using CActiveDataProvider
I think you're experiencing a problem here for not having specified a keyField in your data provider. Try array('keyField'=>'ids) when creating the data provider instance. See also this answer.

How to set a default sort for WordPress post types

I have made a post type, "events", that use a custom field, "Datum" (Swedish for date). I want the default view for this post type to be sorted by this field.
So far I got:
register_post_type(
'events',
array(
'labels' => array(
'name' => __( 'Arrangemang' ),
'singular_name' => __( 'Arrangemang' )
),
'supports' => array(
'title',
'editor',
'custom-fields'
),
'public' => true,
'has_archive' => true,
'query_var' => array(
'post_type' => 'events',
'meta_key' => 'Datum',
'orderby' => 'meta_value',
'order' => 'ASC',
),
)
);
But the ordering doesn't work. If I on the archive page for events run print_r($wp_query->query_vars); I get:
Array
(
[post_type] => events
[error] =>
[m] => 0
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[comments_popup] =>
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[fields] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[meta_query] => Array
(
)
[ignore_sticky_posts] =>
[suppress_filters] =>
[cache_results] => 1
[update_post_term_cache] => 1
[update_post_meta_cache] => 1
[posts_per_page] => 100
[nopaging] =>
[comments_per_page] => 50
[no_found_rows] =>
[order] => DESC
[orderby] => wp_posts.post_date DESC
)
Have I misunderstood the argument query_var for register_post_type, or why isn't is sorting the list as I asked for?
As far as I understand, query_var can only be a boolean and doesn't deal with the ordering of custom post types. A solution would be to make a custom archive page for your post type and modify the loop there so that the archive pages for that custom post type are always sorted by the meta value.
I tried modifying the archive page, but that just ended up quering the posts twice, so I hooked up the "parse_query" to add the order by info if the query was the right one.
I may need to define the right one better later, but right now it works fine, and the query is running just once.
add_action('parse_request', 'event_sort');
function event_sort($a)
{
if($a->query_vars AND count($a->query_vars) == 1 AND $a->query_vars['post_type'] == 'events')
{
$a->query_vars['meta_key'] = 'Datum';
$a->query_vars['orderby'] = 'meta_value';
$a->query_vars['order'] = 'ASC';
}
}
Before I found parse_request I used parse_query:
add_action('parse_query', 'event_sort');
function event_sort($a)
{
if($a->query AND count($a->query) == 1 AND $a->query['post_type'] == 'events')
{
global $wp_query;
$wp_query->set('meta_key', 'Datum');
$wp_query->set('orderby', 'meta_value');
$wp_query->set('order', 'ASC');
}
}

Categories