Change post query onclick

Yes, if you need to run WordPress/PHP functions from a button click (Javascript), you need Ajax.

In the following example, a shortcode will render a button (id="newquote") that calls an Ajax action hook (get_random_cpt). A query is done and replaces one div from that shortcode (id="randomquotes") with the results.

How to Use Ajax in a WordPress Shortcode?