Using ajax with paging and a custom sub-query

Two options- load a page fragment using jQuery’s load method (see Loading Page Fragments), or create your own ajax function to load posts and return it in whatever markup you wish: add_action(‘wp_ajax_my_ajax_get_posts’, ‘my_ajax_callback’); add_action(‘wp_ajax_nopriv_my_ajax_get_posts’, ‘my_ajax_callback’); function my_ajax_callback() { $args = $_POST[‘myargs’]; $collected_child_posts = new WP_Query($args); echo ‘some stuff’; die(); } pass the admin-ajax url to … Read more

query in category.php repeats itself

You should take the condition outide the loop meaning above <?php if (have_posts()) : while (have_posts()) : the_post(); ?> Therewise every time the loop begins it start the check instead of doing the check then starting the loop if the condition exist. Its hard for to understand because i dont understand div names but i … Read more

Custom Query num_rows returns wrong amount

If you want the total count of all the published posts in a particular category you can try this. $all_posts = new WP_Query( array( ‘posts_per_page’ => -1, ‘post_status’ => ‘publish’, ‘cat’ => 3 ) ); echo $all_posts->post_count; or $all_posts = new WP_Query( array( ‘posts_per_page’ => -1, ‘post_status’ => ‘publish’, ‘category_name’ => ‘uncategorized’ ) ); echo … Read more

Exclude featured image and custom field from this get_attachment query

The asker refused to post his solution as answer, so I took the liberty to get this question out of our growing Unanswered Questions list … <?php $logo = get_post_meta($post->ID, ‘_lm_comm_logo’, true); $map = get_post_meta($post->ID, ‘_lm_comm_map’, true); $args = array( ‘numberposts’ => -1, ‘orderby’ => ‘menu_order’, ‘order’ => ‘ASC’, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’, … Read more

$paged always 0 on plugin’s custom page

I resolved this myself. The problem being: add_rewrite_rule(‘^foobar?’,’index.php?is_foobar_page=1′,’top’); Which prevents any other query_vars from being created. To resolve this, a slightly more complicated rewrite rule combo is set up: add_rewrite_rule(‘^foobar?$’,’index.php?is_foobar_page=1′,’top’); add_rewrite_rule(‘foobar/page/([0-9]+)?$’,’index.php?is_foobar_page=1&paged=$matches[1]’,’top’); This takes into consideration the creation of the plugin’s subpage ‘foobar’ and also allows for pagination within this page. Hope this helps someone.

Querying both pages and posts

Use get_posts() or filter ‘pre_get_posts’ on the front page. Sample code for get_posts(): $my_post_array = get_posts( array ( ‘post_type’ => array ( ‘post’, ‘custom_type’ ) ) ); foreach ( $my_post_array as $post ) { // do something awesome … } There are also parameters for a custom order by meta fields and so on. A … Read more

Query get post,how to add comment box

First of all, welcome to WPSE! By default, the comment box does not come with the post object, what you are looking for is the comments_template function. It should be as simple as changing your code to this: <?php $post_id = 104; $queried_post = get_post($post_id); $content = $queried_post->post_content; $content = apply_filters(‘the_content’, $content); $content = str_replace(‘]]>’, … Read more