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.
Related
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 ); ?>
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
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'.
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,
);
I am using the WordPress plug-ins Advanced Custom Fields, and Custom Post Type UI.
I have built a WP_Query to display a particular post type, filtered by a particular custom field value.
$loop = new WP_Query( array(
'post_type' => 'news',
'meta_key' => 'news_story_type',
'meta_value' => 'release',
'posts_per_page' => 3
) );
I now want to sort the resulting posts by another custom field, date_of_publication rather than use WordPress's menu_order or date. The ACF documentation says to specify orderby and meta_key in the query args.
$args = array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'DESC' );
But alas, doing so conflicts with the meta_key I've already supplied to filter.
Has anyone encountered this before and found a solution?
Try using meta_query
$loop = new WP_Query( array(
'post_type' => 'news',
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'posts_per_page' => 3,
'meta_query' => array(
array('key' => 'news_story_type', 'value' => 'release')
)
) );