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:

    [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:

    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.

1 Answer 1


In PHP, the number 0 is interpreted as false-ish. Your if-statement asks for a true-ish condition like this:

If the result of the array_search function is interpreted as true-ish, THEN do the stuff

which means if the result is 0, it is interpreted as false, and the code doesn't run.

What to do?

Use the identical operator for false like this to check for your wanted condition. Change your if-statement like this:

if ( !( false === array_search( $currentpage, $yearlyarchives, true) ) ){
     ?> en el <?php echo get_query_var('year');

The code will then run if the result of the array search is not identical to false (array_search will return false if the needle is not found).

Happy Coding!

  • Thank you so much. Not just for giving me the answer, but to take the time to explain in such a useful way. Commented Jun 28, 2022 at 20:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.