Title: Query posts with javascript or inside ajax handler?
Last modified: August 19, 2016

---

# Query posts with javascript or inside ajax handler?

 *  [Mobster](https://wordpress.org/support/users/mobster/)
 * (@mobster)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/query-posts-with-javascript-or-inside-ajax-handler/)
 * I am looking for a way to query specific pages using javascript inside an ajax
   tabbed div.
 * The tabs call function tab1.php, tab2.php, tab3.php and son on. When I insert
   a query inside tab1.php nothing shows up.
 * Any suggestions? Thank You!

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [Mobster](https://wordpress.org/support/users/mobster/)
 * (@mobster)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/query-posts-with-javascript-or-inside-ajax-handler/#post-1160713)
 * Here is the function. It’s uses jquery-1.2.3.pack.js
 *     ```
       <script type="text/javascript">
       			var pageUrl = new Array();
       			pageUrl[1] = "wp-content/themes/Million/tab1.php";
       			pageUrl[2] = "wp-content/themes/Million/tab2.php";
       			pageUrl[3] = "wp-content/themes/Million/tab3.php";
       			pageUrl[4] = "wp-content/themes/Million/tab4.php";
       			pageUrl[5] = "wp-content/themes/Million/tab5.php";	
   
       			function loadTab(id)
       			{
       				if (pageUrl[id].length > 0)
       				{
       					$("#preloader-ajax").show();
       					$.ajax(
       			        {
       			            url: pageUrl[id],
       			            cache: false,
       			            success: function(message)
       			            {
       			                $("#tabcontent").empty().append(message);
       			                $("#preloader-ajax").hide();
       			            }
       			        });
       				}
       			}
   
       			$(document).ready(function()
       			{
       				$("#preloader-ajax").hide();
   
       				$("#tab1").click(function()
       				{
       					loadTab(1);
       				});
   
       				$("#tab2").click(function()
       				{
       					loadTab(2);
       				});
   
       				$("#tab3").click(function()
       				{
       					loadTab(3);
       				});
   
       				$("#tab4").click(function()
       				{
       					loadTab(4);
       				});
       			});
       		</script>
       ```
   
 *  Thread Starter [Mobster](https://wordpress.org/support/users/mobster/)
 * (@mobster)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/query-posts-with-javascript-or-inside-ajax-handler/#post-1160760)
 * This would be tab3.php but for some reason it will not query the posts once it’s
   called through ajax.
 *     ```
       <script type="text/javascript">
       function instertContent($pagename){
       	$temp = $wp_query;
       	$wp_query = null;
       	$wp_query = new WP_Query();
       	$wp_query->query('pagename='.$pagename.'&static=true');
       	while ($wp_query->have_posts()) : $wp_query->the_post();
       		apply_filters('the_content', the_content());
       	endwhile;
       	$wp_query = null; $wp_query = $temp;
       	rewind_posts();
       }
       function instertExcerpt($pagename){
       	$temp = $wp_query;
       	$wp_query = null;
       	$wp_query = new WP_Query();
       	$wp_query->query('pagename='.$pagename.'&static=true');
       	while ($wp_query->have_posts()) : $wp_query->the_post();
       		apply_filters('the_excerpt', the_excerpt());
       	endwhile;
       	$wp_query = null; $wp_query = $temp;
       	rewind_posts();
       }
       </script>
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Query posts with javascript or inside ajax handler?’ is closed to new
replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 1 participant
 * Last reply from: [Mobster](https://wordpress.org/support/users/mobster/)
 * Last activity: [16 years, 9 months ago](https://wordpress.org/support/topic/query-posts-with-javascript-or-inside-ajax-handler/#post-1160760)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
