Page ordering by title in WP Admin is not working - php

I want to generate a list of checkboxes in a custom meta field for a custom post type in the Wordpress admin. I am using ACF to create the fields. I can query all pages, but the orderby parameter is not respected. It just outputs them by date_created.
Here is my query:
$pageArgs = array(
'post_type' => 'page',
'posts_per_page' => -1,
'nopaging' => true,
'orderby' => 'title',
'order' => 'ASC'
);
$pageArray = array();
$pageQuery = new WP_Query($pageArgs);
if($pageQuery->have_posts()) {
while($pageQuery->have_posts()) {
$pageQuery->the_post();
$pageArray[get_the_permalink()] = get_the_title();
}
}
You can see I create an empty array and enter the values from the query into the array during the loop. If I dump the array after the loop, the orderby parameter is not reflected. Moreover, if I dump the query object, it shows the SQL query and it is ordering by menu_order still.
$pageQuery dump:
...
public 'request' => string 'SELECT SQL_CALC_FOUND_ROWS wp_8_posts.ID
FROM wp_8_posts
WHERE 1=1 AND wp_8_posts.post_type = 'page' AND ((wp_8_posts.post_status = 'publish'))
ORDER BY wp_8_posts.menu_order, wp_8_posts.post_date DESC LIMIT 0, 10'
...
Is this because the query is made in admin? I can certainly sort the array, once the loop is complete, but in an effort to understand WP a little better, Id like to know why this is happening.
Also, I can query this and it lists the pages properly....
$args = array(
'authors' => '',
'child_of' => 0,
'date_format' => get_option('date_format'),
'depth' => 0,
'echo' => 1,
'exclude' => '',
'include' => '',
'link_after' => '',
'link_before' => '',
'post_type' => 'page',
'post_status' => 'publish',
'show_date' => '',
'sort_column' => 'post_title',
'title_li' => __('Pages'),
'walker' => ''
);
var_dump(wp_list_pages( $args ));
which dumps the page names in alpha order.

This is a mixture of two mistakes.
My query was very wrong, as well as the ACF documentation. It should be
'post_type' => 'page',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'meta_key' => 'title',
'order' => 'ASC'
ACF docs say it should be 'meta_key_num', but that did not work for me. I could order by the custom ACF field using 'meta_value'.

Related

Wordpress orderby meta values in array

I'm trying to get sorted list of posts using get_posts by meta value and the order of meta value is given in array.
This is what I currently have.
$stores = get_posts(array(
'post_type' => 'stores',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids', // I only need the ID's of posts
'orderby' => 'meta_value',
'meta_key' => 'state',
'order' => 'ASC'
));
This returns the array of posts sorted by meta_value in ASCENDING alphabetical order.
I have an array of possible values for 'meta_key' => 'state', i.e. array('State1', 'State2', 'State3')
I want to set order so that all stores which has meta value State1 appears first, then from State2 and after that State3
I can't use order by numeric value and alphabetical value as state names are gonna be random.
I found one post here, it is using mera_query_orderby. I can't find any documentation for this and tried it, but it's not working. It returns posts ordered by ID.
Any help would be appreciated. Thanks
EDIT:
I added the meta_query_orderby filters in functions.php
And the updated code I used from EXAMPLE 2, is like:
$stores = get_posts(array(
'post_type' => 'stores',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'state', // Custom field key.
'value' => array("CState1", "AState2", "BState3")
)
),
'meta_query_orderby' => array(
array(
'key' => 'state', // (required) Custom field key.
'value' => array("CState1", "AState2", "BState3")
)
)
));
I have an array of possible values for 'meta_key' => 'state', i.e.
array('State1', 'State2', 'State3')
If you want to sort the posts by the meta value in the exact order as in the above array, you can use a custom WP_Query parameter (to set the meta/sort values) and the posts_orderby filter to customize the ORDER BY clause, and in that clause, you would be using the FIELD() function in MySQL.
Step 1
Add this code to your plugin or theme (if theme, you'd add the code to the theme functions file):
add_filter( 'posts_orderby', 'posts_orderby_meta_value_list', 10, 2 );
function posts_orderby_meta_value_list( $orderby, $query ) {
$key = 'meta_value_list';
if ( $key === $query->get( 'orderby' ) &&
( $list = $query->get( $key ) ) ) {
global $wpdb;
$list = "'" . implode( wp_parse_list( $list ), "', '" ) . "'";
return "FIELD( $wpdb->postmeta.meta_value, $list )";
}
return $orderby;
}
Step 2
When making your post queries, set the orderby to meta_value_list and add meta_value_list to the query parameters — if you're using get_posts(), make sure suppress_filters is set to false:
$stores = get_posts( array(
'post_type' => 'stores',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids', // get just the ID's of posts
'meta_key' => 'state',
'orderby' => 'meta_value_list',
'meta_value_list' => array( 'State1', 'State2', 'State3', '' ),
'suppress_filters' => false,
) );
PS: I the added '' to the array so that posts where the metadata is ('') (i.e. exists in the database, but the value is empty) would be placed at the bottom of the results.
Tried and tested working, but note that the above solution is only for single orderby, which means array is not supported.
/*Display posts of type 'stores', ordered by 'state', and filtered to show only states.*/
$args = array(
'post_type' => 'stores',
'meta_key' => 'state',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'state',
'value' => array( 'State1', 'State2', 'State3'),
'compare' => 'IN',
),
),
);
$query = new WP_Query( $args );

Wordpress: sort get_pages by custom field

I have a custom field "weight" with numeric values at Wordpress site. I want to sort pages using this values.
But I have a problem. It's a very old site, and pages is shown using get_pages(). I can change it to get_posts, but if I do it, I will have to rewrite a big amount of code...
So, maybe the easier solution exists? How can I sort my pages using custom field value without changing get_pages to get_posts?
You can sort pages by using standard argument options provided by wordpress.
<?php ` get_pages( $args ); ` ?>
<?php ` $args = array(
`sort_order` => `asc`,
'sort_column' => 'post_title',
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'meta_key' => '',
'meta_value' => '',
'authors' => '',
'child_of' => 0,
'parent' => -1,
'exclude_tree' => '',
'number' => '',
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages($args); `
?>
meta_key
(string) Only include pages with this meta key.
meta_value
(string) Only include pages with this meta value. Requires $meta_key.
sort_column
(string) What columns to sort pages by, comma-separated. Accepts 'post_author', 'post_date', 'post_title', 'post_name', 'post_modified', 'menu_order', 'post_modified_gmt', 'post_parent', 'ID', 'rand', 'commentcount'. 'post' can be omitted for any values that start with it. Default 'post_title'.
You can sort pages with custom field value.. but you have to change get_pages to get_posts
i think wordpress not provide method to sort pages according to custom field with get_pages function.
$listingPages = get_posts(
array(
'meta_key' => 'streetAdd',
'post_type' => 'page',
'post_status' => 'publish',
'orderby' => 'meta_value',
'order' => 'ASC',
)
);
If you don't want to use get_posts unfortunately there is no option for orderby and order in get_pages.
Check the documentation for get_pages() and for get_posts()
For get_pages the query will be like this
$args = array(
'post_type' => 'page',
'post_status' => 'publish',
'meta_key' => 'weight',
);
$listingPages = get_pages( $args );
But I personally feel that you should use get_posts because it has many parameters for filter. Which will be suitable for condition.

Get posts by category and language, using PolyLang

I'm creating a plugin and I already could get the posts by category and by the current language using get_posts() function from WordPress and passing the attribute lang with the pll_current_language() from PolyLang.
$args = array(
'posts_per_page' => 6,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'lang' => pll_current_language()
);
return get_posts($args);
Now, I'm wondering how to get the posts by categories related to the language?
For example, I have the News category for English and Noticias for Spanish. How can I set this automatically?
Something like this:
$args = array(
......
'category' => **current_category_for_this_language**
......
);
return get_posts($args);
Any ideas?
Use pll_get_term and filter by category. In this case '34' is my term ID (gotten by hovering the edit link of the term).
By the way as far as I know get_posts gets only posts in the current page language by default and it gets posts by default sorted by date DESC, so you could omit those from your query I think.
$args = array(
'posts_per_page' => 6,
'category' => pll_get_term(34)
);
return get_posts($args);
Sources
https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/
pll_get_term
Returns the category (or post tag) translation
Usage:
pll_get_term($term_id, $slug);
‘$term_id’ => (required) id of the term you want the translation
‘$slug’ => (optional) 2-letters code of the language, defaults to current language
https://codex.wordpress.org/Template_Tags/get_posts
Default Usage
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>

Output blog posts only onto a part of webpage

By default, blog posts can be shown on single page.
i want to customize a front page and display the posts only on the part of my page.
If it's possible, how can i output my latest blogposts onto a custom div or element?
Create custom loop, something like this:
http://www.johnmorrisonline.com/how-to-create-a-custom-loop-in-wordpress-using-wp_query/
So, basically use that WP_Query to get list of post and loop trough it:
http://codex.wordpress.org/Class_Reference/WP_Query
Here is an example how I do it:
$args = array(
'posts_per_page' => 3,
'offset' => 0,
'category' => 6,
'category_name' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true );
$posts_array = get_posts( $args );
// print_r($posts_array);
foreach($posts_array as $current_post){
print_r($current_post); // here you have current post values
}
http://codex.wordpress.org/Template_Tags/get_posts

wp_get_recent_posts not working correctly with appended posts

I want to append some posts to the wp_get_recent_posts function, so they are always retrieved with the normal results, the problem is that I dont get the posts I want to append in the results, am I doing something wrong?
$include = array(1, 2);
$args = array(
'showposts' => 10,
'tag_id' => '123',
'post_status' => 'publish',
'exclude' => $current_id,
'orderby' => 'post_date',
'append' => $include,
);
$entries = wp_get_recent_posts($args, 'ARRAY_A');
If I remove the tag_id argument then the posts I want to append are included in the results but I need to filter the normal results by tag ID, seems that WP also filters the appended posts with the other arguments, is there any workaround?
According to https://codex.wordpress.org/Function_Reference/wp_get_recent_posts there is no 'append' parameter, but there is an 'include' parameter that does what you want.
Try this:
$args = array(
'showposts' => 10,
'tag_id' => '123',
'post_status' => 'publish',
'exclude' => $current_id,
'orderby' => 'post_date',
'include' => $include,
);

Categories