Get permalink of the oldest post in wordpress - php

I have kept a Prev-Next option in the single post page to navigate though the posts and this is what I'm using for the next button.
<?php echo get_permalink(get_adjacent_post(false,'',false)); ?>
But I can't figure out a way to link this same button to the very first posts when there are no more new posts to show. The reason is that the posts are used to show products.
Please note: To link Prev button in oldest post to the newest post I have used used this code.
<?php $next_page=get_permalink(get_adjacent_post(false,'',true));
$current_page=get_permalink();
if($next_page==$current_page){
$args = array( 'numberposts' => '1', 'category' => CAT_ID );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo get_permalink($recent["ID"]);
}
} else {
echo $next_page;
}
?>

add the args to query whatever posts are part of the products.
$posts_array = get_posts( $args );
get_permalink($posts_array[0]->ID); // First posts;
$args should be something like this (make sure it returns all the products posts):
$args = array(
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'ASC',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);

Related

Wordpress - post_category shows no result

i am trying to create a small overview over the latest posts of my page. Everything works well with the "wp_get_recent_posts" query. Now i was trying to add some icons to the title, but it always shows me no result, as soon as i try to get the post_category of a post.
If tried to change the $args 'category' => to '1,2,3,4,...' but it didn't helped.
Any advice is highly appreciated. My code:
<?php
$args = array(
'numberposts' => 5,
'offset' => 0,
'category' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' =>'',
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
foreach($recent_posts as $post):
if($post['post_category'] == 1):
echo "<p><i class=\"fas fa-book\"> </i>{$post['post_title']}<br></p>";
elseif($post['post_category'] == 2):
echo "<p><i class=\"fas fa-desktop\"> </i>{$post['post_title']}<br></p>";
endif;
echo $post['post_category']; //Debug, no output.
echo $post['post_title']; //Debug, output: "example post"
echo $post['post_date']; //Debug, output: "2019-05-21"
endforeach;
?>
The post object does not have a post_category property: https://codex.wordpress.org/Class_Reference/WP_Post
You could use get_the_category function with the post ID (https://developer.wordpress.org/reference/functions/get_the_category/):
<?php
$args = array(
'numberposts' => 5,
'offset' => 0,
'category' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' =>'',
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
foreach($recent_posts as $post):
$categories = get_the_category($post['ID']);
foreach ($categories as $category):
if ($category->cat_name == 'firstcategory'):
//first category found
var_dump($category->cat_name);
endif;
if ($category->cat_name == 'secondcategory'):
//second category found
var_dump($category->cat_name);
endif;
endforeach;
endforeach;
?>

How i can get the first post of current category and echo the link?

I trying create a code that will retrieve the first post from a category and echo the link.
But, the code always getting permalink of current post. Can anyone help me to fix it?
<?php
global $post;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
$array = new WP_Query(array(
'category__in' => array($category),
'post_per_page' => 1,
'order' => 'asc',
'orderby' => 'id'
));?>
<?php the_permalink($post->ID); ?>
$args = array(
'numberposts' => 1,
'orderby' => 'post_date',
'order' => 'ASC',
'fields' => 'ids',
'category' => 8 //or use further logic to check in list
);
$post_cus = get_posts($args);
$first_post_id = $post_cus[0];
$post_url = get_the_permalink ($first_post_id);

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

Posts by admin on the top from all the post?

I am retrieving some post and to show post on first come first basic. Now I want to show those post on the top which are posted by admin and the rest post by non admin will come after admin posts.
the code I am using in php is:
$tit = get_the_title();
$args = array(
'post_type' =>'contribute',
'numberposts' => 100,
'meta_key' => 'portfolio',
'meta_value' => $tit ,
);
$slides = get_posts($args);
?>
<ul id="myList">
<?php foreach($slides as $post) : setup_postdata($post); ?>
<li>the post will go here</li>
<?php endforeach; wp_reset_postdata(); ?>
To display posts from admin first and then from non-admin, you have to call get_posts twice.
Once with :-
$args = array(
'post_type' =>'contribute',
'numberposts' => 100,
'meta_key' => 'portfolio',
'meta_value' => $tit ,
'author' => '123' // where 123 is ID of your admin author
);
The other will be :-
$args = array(
'post_type' =>'contribute',
'numberposts' => 100,
'meta_key' => 'portfolio',
'meta_value' => $tit ,
'author' => '-123' // display posts except admin author
);
Add the author method to your array:
$args = array(
'post_type' =>'contribute',
'numberposts' => 100,
'meta_key' => 'portfolio',
'meta_value' => $tit,
'author_name' => 'Administrator' //add this, change Administrator to your name
);

Categories