You should defenetley not do what you try to do in your code. To be able to get all the years from a "select *"-query you have to go through all the posts in the table. What if you want to return dates, and have a table with millions of rows, then you will have a big problem. Instead you should only return the unique years, read up on distinct and group by to learn this.
You should use only one query instead of two, something simular to this:
select distinct(year) as year from (
select distinct(year) as year from table1
union
select distinct(year) as year from table2
)
It is possible that you should use group by instead of distinct.