Let's call the functions defined by Ali Taghavi to be sliced functions: a continuous function $\ f:\mathbb R^2\rightarrow\mathbb R\ $ is called sliced $\ \Leftarrow:\Rightarrow\ \ \forall_{c\in\mathbb R}\ f^{-1}(c)\ $ is convex.
NOTATION:
$$\ [x;y]\ :=\ \{(1\!-\!t)\cdot x\ +\ t\cdot y\ :\ 0\le t\le 1\}\ $$
for arbitrary $\ x\ y\in \mathbb R^n\ $ and $\ n=0\ 1\ 2\ \ldots.\ $ Thus $\ [x;y]=[y;x]\ $ in every dimension including $\ n=1$.
THEOREM 0 Let $\ f:\mathbb R^2\rightarrow \mathbb R\ $ be sliced. Then $\ f^{-1}(C)\ $ is convex for every convex $\ C\subseteq\mathbb R$.
PROOF Let $\ C\subseteq \mathbb R\ $ be convex. Let $\ a\ b\in C.\ $ We want to show that
$$\ [a;b]\ \subseteq\ C $$
If $\ f(a)=f(b)\ $ then the above holds due to the convex part of the definition of a sliced function.
Now, assume $\ f(a)\ne f(b).\ $ Then $\ f^{-1}([f(a);f(b)])\ \cap\ [a;b]\ $ is a closed subset of $\ [a;b].\ $ Next, consider arbitrary $\ x\ y \in [a;b]\cap f^{-1}([f(a);f(b)],\ $ and $\ x\ne y.\ $ If $\ f(x)=f(y)\ $ then again
$$ [x;y]\ \subseteq f^{-1}(x)\ \subseteq\ f^{-1}([f(a);f(b)]) $$
And if $\ f(x)\ne f(y)\ $ then, due to continuity of $\ f\ $ (and of the nature of $\ \mathbb R$) we have
$$ f([x;y])\ \supseteq\ [f(x);f(y)] $$
Thus there exists $\ w\in[x;y]\ $ such that
$$ f(w)\ =\ \frac {f(x)+f(y)}2 $$
We see that $\ w\in(x;y)\ $ belongs to the interior of the interval, and $\ w\in f^{-1}[f(a);f(b)].\ $ This shows that $[a;b]\cap f^{-1}([f(a);f(b)])\ $ is dense in $\ [a;b],\ $ hence $\ [a;b]\subset f^{-1}(C).\ $ END of PROOF
After this exercise we get:
THEOREM 1 Let $\ f:\mathbb R^2\rightarrow \mathbb R\ $ be sliced. Then there exists $\ (a\ b)\in\mathbb R^2\setminus\{(0\ 0)\}\ $ such that:
$$\forall_{(x\ y)\,\ (x'\ y')\,\in\,\mathbb R^2 }\ \ \left(\ a\cdot x+b\cdot y = a\cdot x'+b\cdot y'\ \ \Rightarrow\ \ f(x\ y)=f(x'\ y')\ \right)$$
PROOF The case of a constant function is trivial. Otherwise there exists $\ h\in\mathbb R\ $ such that both sets $\ f^{-1}((-\infty\;h))\ $ and $\ f^{-1}((h;\infty))\ $ are non-empty. Then these two sets are disjoint open half-planes, i.e. they are non-empty, open and convex, and the complement of each of them is non-empty, closed and convex. Thus there exist $\ s\ t\in\mathbb R\ $ and $\ (a\ b)\in\mathbb R^2\setminus\{(0\ 0\}\ $ such that $\ s<t\ $ and
$$f(x\ y) = h\quad\Leftrightarrow\quad s\ \le\ a\cdot x+b\cdot y\ \le\ t$$
Then this $\ (a\ b)\ $ is the one required by the theorem. END of PROOF
This is about all about the general structure of the sliced functions.
A sliced function is constant in the direction perpendicular to the vector $\ (a\ b)\in\mathbb R\setminus\{(0\ 0)\}\ $ (see above), and it is monotone along that direction: let $\ \phi:\mathbb R\rightarrow\mathbb R\ $ be given by:
$$\forall_{u\in\mathbb R}\ \ \phi(u)\ :=\ f\left(u\cdot(a\ b)\right)$$
Then $\ \phi\ $ is monotonne. Thus each slide function is differentiable almost everywhere.
Let me resolve a special case of the question, by leveraging the examples mentioned above. Suppose $(K,\rho)$ is a compact metric space and $X$ is the set of Borel probability measures on $K$ endowed with its weak* topology. I claim that $X$ has the convex function property if and only if $K$ is finite.
Theorem 10.2 in "Convex Analysis" by Rockafellar implies that any convex function defined on a finite-dimensional simplex is upper semicontinuous. This gives one direction.
Conversely, suppose $K$ is infinite. Letting $k_\infty$ be an accumulation point of $K$ (which exists because $K$ is an infinite compact metrizable space), define the affine continuous function $\varphi:X\to\mathbb R^2$ given by $\varphi(x):=\int_K \left(\rho(k,k_\infty), \rho(k,k_\infty)^2\right)\text{ d}x(k)$. Then, the convex function $f:X\to\mathbb R$ which takes $f(\delta_{k_\infty}):=0$ and $f(x):=\tfrac{\varphi_1(x)^2}{\varphi_2(x)}$ for every $x\neq\delta_{k_\infty}$ should work. A failure of continuity is witnessed along sequence $(\delta_{k_n})_{n=1}^\infty$ where, $\{{k_n}\}_{n=1}^\infty\subseteq K\setminus\{k_\infty\}$ is a sequence converging to $k_\infty$.
A natural conjecture is now that a general $X$ will have the convex function property if and only if $X$ has finitely many extreme points. Is this true?
Best Answer
Assume that $g$ defined on $\mathbb{Q}^n$ is midpoint convex. First we show that we can extend the midpoint inequality to arbitrary means:
$g((x_1+\dots+x_m)/m)\leq (g(x_1)+\dots+g(x_m))/m$ for any $m\in\mathbb{Z}_{\geq1}$
We can easily prove this for $m=2^k$ by using midpoint convexity $k$ times.
For general $m\leq 2^{i}$ you take $x_1,\dots,x_m$ plus $2^i-m$ copies of $x'=(x_1+\dots+x_m)/m$
This gives $$g(x')=g\left(\frac{(2^i-m)x'+x_1+\cdots+x_m}{2^i}\right) \le\frac{(2^i-m)g(x')+g(x_1)+\dots+g(x_m)}{2^i},$$ which implies $g(x')=g((x_1+\dots+x_m)/m) \leq (g(x_1)+\dots+g(x_m))/m$ for any $m\in\mathbb{Z}_{\geq1}$.
Finally, taking $a$ copies of $x$ and $b$ copies of $y$ we obtain $$g(\frac{ax+by}{a+b})\leq \frac{ag(x)+bg(y)}{a+b}=\left[\frac{a}{a+b}\right]g(x)+\left[\frac{b}{a+b}\right]g(y)$$ for $a,b\in \mathbb{Z}_\geq0$ not both zero and hence that $g$ is rationally convex.