Get posts by category and language, using PolyLang - php

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 ); ?>

Related

Show posts and custom post types

Helllo,
I want to show posts and custom post types , can I change post_type in query as array('post','custom_post_type')
function get_posts( $args = null ) {
$defaults = array(
'numberposts' => 5,
'category' => 0,
'orderby' => 'date',
'order' => 'DESC',
'include' => array(),
'exclude' => array(),
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'suppress_filters' => true,
);
thanks in advance
You can add the post_type as a string (single type) or an array (multiple types).
Example:
$args = array(
'post_type' => array( 'post', 'page', 'movie', 'book' )
);
movie and book above are Custom Post Types.
If you want all post types just:
$args = array(
'post_type' => 'any'
);
The above retrieves any type except revisions and types with exclude_from_search set to true.
More info here
<?php
$args = array(
'post_type' => 'my_post_type',
'post_status' => 'publish',
'posts_per_page' => -1
);
?>
If you want to fetch the WordPress posts you can write post in the post_type parameter. If you want to fetch the custom post type you can pass the name of the custom post type like movies or courses, to match whatever your custom post type name is in WordPress.

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.

mysql query wp_post list and category and date

I need to get a list of all post from one category and since some date until today.
The problem is that i dont find a way to ask this to google & im starting using mysql...
I think that it will be something like:
SELECT * FROM `wp_posts` WHERE 1 and the rest...
Use wordpress built in function to get posts
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'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 ); ?>
If you need to get it from date till now, use class "WP_Query":
$args = array(
'date_query' => array(
array(
'after' => array(
'year' => 2015,
'month' => 1,
'day' => 31,
),
'inclusive' => true,
),
),
'category_name' => 'category',
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
This should give you clue where to continue. For more detailed examples follow Wordpress codex: http://codex.wordpress.org/Class_Reference/WP_Query

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