I made (copy) a function to make the url of the custom post (named investigacion) by year.
function tex_get_archives($post_type = 'investigacion') {
global $wpdb;
$query2 = "SELECT YEAR(post_date) AS `year` FROM $wpdb->posts WHERE `post_type` = '$post_type' AND `post_status` = 'publish' GROUP BY `year` ORDER BY `year` DESC";
$arcresults = $wpdb->get_results($query2);
$years = array();
if ($arcresults) {
foreach ( (array)$arcresults as $arcresult ) {
array_push($years, $arcresult->year);
}
}
$year_archives = [];
foreach ($years as $year) {
if ( $year ) {
$url = get_year_link( $year );
$year_archives[] = $url.'?post_type='.$post_type;
}
}
return $year_archives;
}
This function returns:
Array
(
[0] => http://ah6studio.local/2022/?post_type=investigacion
[1] => http://ah6studio.local/2020/?post_type=investigacion
[2] => http://ah6studio.local/2018/?post_type=investigacion
[3] => http://ah6studio.local/2010/?post_type=investigacion
[4] => http://ah6studio.local/2006/?post_type=investigacion
)
So, that's ok. Then, I wanted to add some code only if you're in a yearly archive, in any of those url. So I used an if statemente that search if the url of the current page is on the array, it would add a text saying what year is the archive we are on:
<?php
$yearlyarchives=tex_get_archives();
global $wp;
$currentpage= home_url( $wp->request )."/?post_type=investigacion";
if (array_search($currentpage,$yearlyarchives,true)){
?> en el <?php echo get_query_var('year');
}
But, it runs true when I'm in all the archive pages except the first one [0], the 2022 one. It runs false (I tested it). The array_search works just not with the first element. Is really weird.