Ainsley Clark
Forum Replies Created
-
Forum: Developing with WordPress
In reply to: Posts Per Page -> Per Post TypeI dont have two post tables, your right! Dont know where that came from.
Now this:
WordPress database error: [Unknown column ‘primary_foe_342fj29x2_posts.id’ in ‘field list’]
Think its coming from this line:
$whereTemplate[] = "primary_foe_342fj29x2_posts.post_type = post_type_$post_type_key.post_type";
WordPress database error: [Unknown column 'primary_foe_342fj29x2_posts.id' in 'field list'] select count(primary_foe_342fj29x2_posts.id) from foe_342fj29x2_posts left join (select GROUP_CONCAT(id order by post_date desc, id desc) as grouped_id, post_type from ( SELECT id, post_type, post_date FROM foe_342fj29x2_posts inner join foe_342fj29x2_term_relationships on foe_342fj29x2_term_relationships.object_id = id inner join foe_342fj29x2_term_taxonomy on foe_342fj29x2_term_relationships.term_taxonomy_id = foe_342fj29x2_term_taxonomy.term_taxonomy_id inner join foe_342fj29x2_terms on foe_342fj29x2_term_taxonomy.term_id = foe_342fj29x2_terms.term_id where foe_342fj29x2_term_taxonomy.taxonomy = 'foebar' and foe_342fj29x2_terms.slug = 'marketing-experts' and post_type in ('news') and post_status = 'publish' order by post_date desc, id desc LIMIT 18446744073709551610 offset 0 ) d0 GROUP BY d0.post_type) post_type_0 ON primary_foe_342fj29x2_posts.post_type = post_type_0.post_type left join (select GROUP_CONCAT(id order by post_date desc, id desc) as grouped_id, post_type from ( SELECT id, post_type, post_date FROM foe_342fj29x2_posts inner join foe_342fj29x2_term_relationships on foe_342fj29x2_term_relationships.object_id = id inner join foe_342fj29x2_term_taxonomy on foe_342fj29x2_term_relationships.term_taxonomy_id = foe_342fj29x2_term_taxonomy.term_taxonomy_id inner join foe_342fj29x2_terms on foe_342fj29x2_term_taxonomy.term_id = foe_342fj29x2_terms.term_id where foe_342fj29x2_term_taxonomy.taxonomy = 'foebar' and foe_342fj29x2_terms.slug = 'marketing-experts' and post_type in ('speakers') and post_status = 'publish' order by post_date desc, id desc LIMIT 18446744073709551610 offset 0 ) d1 GROUP BY d1.post_type) post_type_1 ON primary_foe_342fj29x2_posts.post_type = post_type_1.post_type left join (select GROUP_CONCAT(id order by post_date desc, id desc) as grouped_id, post_type from ( SELECT id, post_type, post_date FROM foe_342fj29x2_posts inner join foe_342fj29x2_term_relationships on foe_342fj29x2_term_relationships.object_id = id inner join foe_342fj29x2_term_taxonomy on foe_342fj29x2_term_relationships.term_taxonomy_id = foe_342fj29x2_term_taxonomy.term_taxonomy_id inner join foe_342fj29x2_terms on foe_342fj29x2_term_taxonomy.term_id = foe_342fj29x2_terms.term_id where foe_342fj29x2_term_taxonomy.taxonomy = 'foebar' and foe_342fj29x2_terms.slug = 'marketing-experts' and post_type in ('exhibitors') and post_status = 'publish' order by post_date desc, id desc LIMIT 18446744073709551610 offset 0 ) d2 GROUP BY d2.post_type) post_type_2 ON primary_foe_342fj29x2_posts.post_type = post_type_2.post_type where ((primary_foe_342fj29x2_posts.post_type = post_type_0.post_type ) or (primary_foe_342fj29x2_posts.post_type = post_type_1.post_type ) or (primary_foe_342fj29x2_posts.post_type = post_type_2.post_type )) group by primary_foe_342fj29x2_posts.post_type
Forum: Developing with WordPress
In reply to: Posts Per Page -> Per Post TypeBeen fiddling for a while now, Im up to this:
News for Business Leaders:
Notice: Undefined index: cache_results in /Users/localhost/website/wp-content/themes/theme/functions.php on line 1160
I can see the pagination now though, but no posts.
Im using this query:
foreach ($post_types as $post_type_key => $post_type) { $joinTemplate[] = "left join (select GROUP_CONCAT(id order by post_date desc, id desc) as grouped_id, post_type from ( SELECT id, post_type, post_date FROM foe_342fj29x2_posts inner join foe_342fj29x2_term_relationships on foe_342fj29x2_term_relationships.object_id = id inner join foe_342fj29x2_term_taxonomy on foe_342fj29x2_term_relationships.term_taxonomy_id = foe_342fj29x2_term_taxonomy.term_taxonomy_id inner join foe_342fj29x2_terms on foe_342fj29x2_term_taxonomy.term_id = foe_342fj29x2_terms.term_id where foe_342fj29x2_term_taxonomy.taxonomy = 'foebar' and foe_342fj29x2_terms.slug = '$post_slug' and post_type in ('$post_type') and post_status = 'publish' order by post_date desc, id desc LIMIT 18446744073709551610 offset #sql_offset# ) d$post_type_key GROUP BY d$post_type_key.post_type) post_type_$post_type_key ON primary_foe_342fj29x2_posts.post_type = post_type_$post_type_key.post_type"; $whereTemplate[] = "primary_foe_342fj29x2_posts.post_type = post_type_$post_type_key.post_type"; $whereTemplateAddon[] = "AND (FIND_IN_SET(primary_foe_342fj29x2_posts.id, post_type_$post_type_key.grouped_id) <= $post_per_posttype and FIND_IN_SET(primary_foe_342fj29x2_posts.id, post_type_$post_type_key.grouped_id) > 0)"; }
- This reply was modified 6 years ago by Ainsley Clark.
Forum: Developing with WordPress
In reply to: Posts Per Page -> Per Post Type@vrandom sorry for all the messages!
Missing bracket was causing it. Now getting all the data dumped out, is it now a case of using this sql query for the jointemplate[]?
Forum: Developing with WordPress
In reply to: Posts Per Page -> Per Post TypeNow on to this:
WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ” at line 19]where ((primary_foe_342fj29x2_posts.post_type = post_type_0.post_type AND (FIND_IN_SET(primary_foe_342fj29x2_posts.id, post_type_0.grouped_id) <= 2 and FIND_IN_SET(primary_foe_342fj29x2_posts.id, post_type_0.grouped_id) > 0))";
Apologies, my SQL is poor!
- This reply was modified 6 years ago by Ainsley Clark.
Forum: Developing with WordPress
In reply to: Posts Per Page -> Per Post TypeRan the second query and got:
WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ” at line 19] will have a look now.
Forum: Developing with WordPress
In reply to: Posts Per Page -> Per Post TypeHi @vrandom
I got the data returning from the first query:
array(1) {
[0]=>
object(stdClass)#6075 (2) {
[“grouped_id”]=>
string(15) “398,397,392,343”
[“post_type”]=>
string(4) “news”
}
}With regards to the primary_ as I changed my wordpress table prefix, I changed all the primary_wp to primary_foe_tableprefix etc. I dont know if this would have something to do with it:
Are you able to tell me where your spotting: So can you explain why you need data from the two tables, foe_342fj29x2_posts primary_foe_342fj29x2_posts?
$post_slug=$post->post_name; global $wpdb; $sql = "select GROUP_CONCAT(id order by post_date desc, id desc) as grouped_id, post_type from ( SELECT id, post_type, post_date FROM foe_342fj29x2_posts inner join foe_342fj29x2_term_relationships on foe_342fj29x2_term_relationships.object_id = id inner join foe_342fj29x2_term_taxonomy on foe_342fj29x2_term_relationships.term_taxonomy_id = foe_342fj29x2_term_taxonomy.term_taxonomy_id inner join foe_342fj29x2_terms on foe_342fj29x2_term_taxonomy.term_id = foe_342fj29x2_terms.term_id where foe_342fj29x2_term_taxonomy.taxonomy = 'foebar' and foe_342fj29x2_terms.slug = '$post_slug' and post_type in ('news') and post_status = 'publish' order by post_date desc, id desc LIMIT 18446744073709551610 offset 0 ) d0 GROUP BY d0.post_type"; $results = $wpdb->get_results($sql); echo "<pre>"; var_dump($results); echo "</pre>"; $post_types = ['news', 'speakers', 'exhibitors']; // number of post to show per each post_type $post_per_posttype = 2; $sql_offset = get_query_var('paged', 0); if ($sql_offset -1 > 0) { $sql_offset = ($sql_offset - 1) * $post_per_posttype; } // Make SQL Parts $joinTemplate = []; $whereTemplate = []; $whereTemplateAddon = []; foreach ($post_types as $post_type_key => $post_type) { $joinTemplate[] = "left join (select GROUP_CONCAT(id order by post_date desc, id desc) as grouped_id, post_type from ( SELECT id, post_type, post_date FROM foe_342fj29x2_posts inner join foe_342fj29x2_term_relationships on foe_342fj29x2_term_relationships.object_id = id inner join foe_342fj29x2_term_taxonomy on foe_342fj29x2_term_relationships.term_taxonomy_id = foe_342fj29x2_term_taxonomy.term_taxonomy_id inner join foe_342fj29x2_terms on foe_342fj29x2_term_taxonomy.term_id = foe_342fj29x2_terms.term_id where foe_342fj29x2_term_taxonomy.taxonomy = '$post_slug' and foe_342fj29x2_terms.slug = '$post_slug' and post_type in ('$post_type') and post_status = 'publish' order by post_date desc, id desc LIMIT 18446744073709551610 offset #sql_offset# ) d$post_type_key GROUP BY d$post_type_key.post_type) post_type_$post_type_key ON primary_foe_342fj29x2_posts.post_type = post_type_$post_type_key.post_type"; $whereTemplate[] = "primary_foe_342fj29x2_posts.post_type = post_type_$post_type_key.post_type"; $whereTemplateAddon[] = "AND (FIND_IN_SET(primary_foe_342fj29x2_posts.id, post_type_$post_type_key.grouped_id) <= $post_per_posttype and FIND_IN_SET(primary_foe_342fj29x2_posts.id, post_type_$post_type_key.grouped_id) > 0)"; } $sql_template = "select #sql_col# from foe_342fj29x2_posts primary_foe_342fj29x2_posts #join_templates_0# #join_templates_1# #join_templates_2# where ((#where_0# #where_addon_0#) or (#where_1# #where_addon_1#) or (#where_2# #where_addon_2#)) #append#"; // Assemble Queries $sqlQuerys['found_post_query'] = $sql_template; $sqlQuerys['wp_query'] = $sql_template; $found_posts_sql = $sql_template; foreach ($post_types as $post_type_key => $post_type) { $sqlQuerys['found_post_query'] = str_replace("#sql_col#", 'count(primary_foe_342fj29x2_posts.id)', $sqlQuerys['found_post_query']); $sqlQuerys['found_post_query'] = str_replace("#append#", 'group by primary_foe_342fj29x2_posts.post_type', $sqlQuerys['found_post_query']); $sqlQuerys['found_post_query'] = str_replace("#where_addon_$post_type_key#", '', $sqlQuerys['found_post_query']); $sqlQuerys['found_post_query'] = str_replace("#join_templates_$post_type_key#", str_replace("#sql_offset#", 0, $joinTemplate[$post_type_key]), $sqlQuerys['found_post_query']); $sqlQuerys['found_post_query'] = str_replace("#where_$post_type_key#", $whereTemplate[$post_type_key], $sqlQuerys['found_post_query']); $sqlQuerys['wp_query'] = str_replace("#sql_col#", '*', $sqlQuerys['wp_query']); $sqlQuerys['wp_query'] = str_replace("#append#", "", $sqlQuerys['wp_query']); $sqlQuerys['wp_query'] = str_replace("#where_addon_$post_type_key#", $whereTemplateAddon[$post_type_key], $sqlQuerys['wp_query']); $sqlQuerys['wp_query'] = str_replace("#join_templates_$post_type_key#", str_replace("#sql_offset#", $sql_offset, $joinTemplate[$post_type_key]), $sqlQuerys['wp_query']); $sqlQuerys['wp_query'] = str_replace("#where_$post_type_key#", $whereTemplate[$post_type_key], $sqlQuerys['wp_query']); } // global $wpdb; // need to pass the max posts possible to the query as it would not be generated correctly $page_count_per_post_type = $wpdb->get_results($sqlQuerys['found_post_query'], ARRAY_N); // get the largest page count on a page $largest_page_count_per_post_type = 0; $found_posts = 0; foreach ($page_count_per_post_type as $page_count) { $largest_page_count_per_post_type = ($page_count[0] > $largest_page_count_per_post_type)? $page_count[0]:$largest_page_count_per_post_type; $found_posts += $page_count[0]; } // page_per_posts is for pagination (post_per_posttype * num_of_posttypes_in_query) $loop = new WP_Query_CustomSQL($sqlQuerys['wp_query'], array( 'posts_per_page' => $post_per_posttype * 3 , 'found_posts' => $found_posts, 'max_num_pages' => ceil( $largest_page_count_per_post_type / $post_per_posttype ))); // put the post in the order of the post_types array if ($loop->have_posts()) { // make posts index by post_type $tmpPosts = []; foreach ($loop->posts as $k => $v) { $tmpPosts[get_post_type( $v->ID )][] = $v; } // assemble new ordered posts $finPosts = []; foreach ($post_types as $k => $v) { if (isset($tmpPosts[$v])) { foreach ($tmpPosts[$v] as $k1 => $v2) { $finPosts[] = $v2; } } } // update the $loop with the new ordered posts $loop->posts = $finPosts; } $speakerCounter = 0; $exhibitorCounter = 0; $columnwidth = 'col-lg-6'; $post_type = get_post_type( $post->ID ); if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); if ($post_type == 'news' && $newsCounter < 2) { $newsCounter++; require( locate_template ('blocks/content-newsrow.php')); echo $post->ID . '] ' . $post_type . ' - ' . get_permalink($post->ID) . "<BR>"; } if ($post_type == 'exhibitors') { if ($exhibitorCounter == 0) { echo '<div class="row"><div class="col-12 mb-2 mb-lg-3">'; ?> <span class="contentbrands__title type__weight--medium">Exhibitors for <?php echo $contentbrand; ?>:</span> </div></div> <?php echo '<div class="row">'; ?> <?php } $exhibitorCounter++; require( locate_template ('blocks/content-exhibitor.php')); echo $post->ID . '] ' . $post_type . ' - ' . get_permalink($post->ID) . "<BR>"; if ($exhibitorCounter == 2) {echo '</div>';} } if ($post_type == 'speakers') { if ($speakerCounter == 0) { echo '<div class="row"><div class="col-12 mb-2 mb-lg-3">'; ?> <span class="contentbrands__title type__weight--medium">Experts for <?php echo $contentbrand; ?>:</span> </div></div> <?php echo '<div class="row">'; ?> <?php } $speakerCounter++; require( locate_template ('blocks/content-speaker.php')); echo $post->ID . '] ' . $post_type . ' - ' . get_permalink($post->ID) . "<BR>"; if ($speakerCounter == 1) {echo '</div>';} } endwhile; // Previous/next page navigation. $args = array( 'prev_text' => __( 'Previous page', 'twentysixteen' ), 'next_text' => __( 'Next page', 'twentysixteen' ), 'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentysixteen' ) . ' </span>', 'screen_reader_text' => __( 'Posts navigation' ), 'type' => 'plain' ); // Set up paginated links. $links = paginate_links_with_provided_wpquery( $args , $loop); if ( $links ) { echo _navigation_markup( $links, 'pagination', $args['screen_reader_text'] ); } ?> <?php else : echo '<h3>No ss</h3>'; endif; wp_reset_postdata(); ?>
- This reply was modified 6 years ago by Ainsley Clark.
Forum: Developing with WordPress
In reply to: Posts Per Page -> Per Post TypeHi @vrandom
Got it echoing this:
array(1) {
[0]=>
object(stdClass)#6075 (3) {
[“id”]=>
string(3) “395”
[“post_type”]=>
string(4) “news”
[“post_date”]=>
string(19) “2019-02-24 09:36:45”
}
}Forum: Developing with WordPress
In reply to: Posts Per Page -> Per Post TypeHi @vrandom,
I must have been using the $post_slug to return the taxonomy, the post slug has the same url as the taxonomy slug. The taxonomy is called foebar, and each has a page and Im using this one page template for all 5 of them, the idea is that each page will pull in speakers, exhibitors and news that are tagged with the particular taxonomy.
In the SQL query I changed ‘category’ to the post slug but still yielding no results. Is there a way to test what the sql query is spitting out?
Not 100% sure about the tax_query portion?
Thanks so much.
- This reply was modified 6 years ago by Ainsley Clark.
Forum: Developing with WordPress
In reply to: Posts Per Page -> Per Post TypeHi @vrandom
Still no luck, the code above successfully got rid of the error, but there are no posts being put in so its not outputting anything.
Currently the query is for a custom taxonomy, Im adding this code to a page template for all of the taxonomy pages.
Im not sure if this is already in place I’m also not sure what should be in the post_slug variable at the top of the query. Maybe its not working because of that? I cant be sure.Would be great if this could get working.
Many thanks again.
$post_slug=$post->post_name; $post_types = ['news', 'speakers', 'exhibitors']; // number of post to show per each post_type $post_per_posttype = 2; $sql_offset = get_query_var('paged', 0); if ($sql_offset -1 > 0) { $sql_offset = ($sql_offset - 1) * $post_per_posttype; } // Make SQL Parts $joinTemplate = []; $whereTemplate = []; $whereTemplateAddon = []; foreach ($post_types as $post_type_key => $post_type) { $joinTemplate[] = "left join (select GROUP_CONCAT(id order by post_date desc, id desc) as grouped_id, post_type from ( SELECT id, post_type, post_date FROM foe_342fj29x2_posts inner join foe_342fj29x2_term_relationships on foe_342fj29x2_term_relationships.object_id = id inner join foe_342fj29x2_term_taxonomy on foe_342fj29x2_term_relationships.term_taxonomy_id = foe_342fj29x2_term_taxonomy.term_taxonomy_id inner join foe_342fj29x2_terms on foe_342fj29x2_term_taxonomy.term_id = foe_342fj29x2_terms.term_id where foe_342fj29x2_term_taxonomy.taxonomy = 'category' and foe_342fj29x2_terms.slug = '$post_slug' and post_type in ('$post_type') and post_status = 'publish' order by post_date desc, id desc LIMIT 18446744073709551610 offset #sql_offset# ) d$post_type_key GROUP BY d$post_type_key.post_type) post_type_$post_type_key ON primary_foe_342fj29x2_posts.post_type = post_type_$post_type_key.post_type"; $whereTemplate[] = "primary_foe_342fj29x2_posts.post_type = post_type_$post_type_key.post_type"; $whereTemplateAddon[] = "AND (FIND_IN_SET(primary_foe_342fj29x2_posts.id, post_type_$post_type_key.grouped_id) <= $post_per_posttype and FIND_IN_SET(primary_foe_342fj29x2_posts.id, post_type_$post_type_key.grouped_id) > 0)"; } $sql_template = "select #sql_col# from foe_342fj29x2_posts primary_foe_342fj29x2_posts #join_templates_0# #join_templates_1# #join_templates_2# where ((#where_0# #where_addon_0#) or (#where_1# #where_addon_1#) or (#where_2# #where_addon_2#)) #append#"; // Assemble Queries $sqlQuerys['found_post_query'] = $sql_template; $sqlQuerys['wp_query'] = $sql_template; $found_posts_sql = $sql_template; foreach ($post_types as $post_type_key => $post_type) { $sqlQuerys['found_post_query'] = str_replace("#sql_col#", 'count(primary_foe_342fj29x2_posts.id)', $sqlQuerys['found_post_query']); $sqlQuerys['found_post_query'] = str_replace("#append#", 'group by primary_foe_342fj29x2_posts.post_type', $sqlQuerys['found_post_query']); $sqlQuerys['found_post_query'] = str_replace("#where_addon_$post_type_key#", '', $sqlQuerys['found_post_query']); $sqlQuerys['found_post_query'] = str_replace("#join_templates_$post_type_key#", str_replace("#sql_offset#", 0, $joinTemplate[$post_type_key]), $sqlQuerys['found_post_query']); $sqlQuerys['found_post_query'] = str_replace("#where_$post_type_key#", $whereTemplate[$post_type_key], $sqlQuerys['found_post_query']); $sqlQuerys['wp_query'] = str_replace("#sql_col#", '*', $sqlQuerys['wp_query']); $sqlQuerys['wp_query'] = str_replace("#append#", "", $sqlQuerys['wp_query']); $sqlQuerys['wp_query'] = str_replace("#where_addon_$post_type_key#", $whereTemplateAddon[$post_type_key], $sqlQuerys['wp_query']); $sqlQuerys['wp_query'] = str_replace("#join_templates_$post_type_key#", str_replace("#sql_offset#", $sql_offset, $joinTemplate[$post_type_key]), $sqlQuerys['wp_query']); $sqlQuerys['wp_query'] = str_replace("#where_$post_type_key#", $whereTemplate[$post_type_key], $sqlQuerys['wp_query']); } // global $wpdb; // need to pass the max posts possible to the query as it would not be generated correctly $page_count_per_post_type = $wpdb->get_results($sqlQuerys['found_post_query'], ARRAY_N); // get the largest page count on a page $largest_page_count_per_post_type = 0; $found_posts = 0; foreach ($page_count_per_post_type as $page_count) { $largest_page_count_per_post_type = ($page_count[0] > $largest_page_count_per_post_type)? $page_count[0]:$largest_page_count_per_post_type; $found_posts += $page_count[0]; } // page_per_posts is for pagination (post_per_posttype * num_of_posttypes_in_query) $loop = new WP_Query_CustomSQL($sqlQuerys['wp_query'], array( 'posts_per_page' => $post_per_posttype * 3 , 'found_posts' => $found_posts, 'max_num_pages' => ceil( $largest_page_count_per_post_type / $post_per_posttype ))); // put the post in the order of the post_types array if ($loop->have_posts()) { // make posts index by post_type $tmpPosts = []; foreach ($loop->posts as $k => $v) { $tmpPosts[get_post_type( $v->ID )][] = $v; } // assemble new ordered posts $finPosts = []; foreach ($post_types as $k => $v) { if (isset($tmpPosts[$v])) { foreach ($tmpPosts[$v] as $k1 => $v2) { $finPosts[] = $v2; } } } // update the $loop with the new ordered posts $loop->posts = $finPosts; } $speakerCounter = 0; $exhibitorCounter = 0; $columnwidth = 'col-lg-6'; $post_type = get_post_type( $post->ID ); if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); if ($post_type == 'news' && $newsCounter < 2) { $newsCounter++; require( locate_template ('blocks/content-newsrow.php')); echo $post->ID . '] ' . $post_type . ' - ' . get_permalink($post->ID) . "<BR>"; } if ($post_type == 'exhibitors') { if ($exhibitorCounter == 0) { echo '<div class="row"><div class="col-12 mb-2 mb-lg-3">'; ?> <span class="contentbrands__title type__weight--medium">Exhibitors for <?php echo $contentbrand; ?>:</span> </div></div> <?php echo '<div class="row">'; ?> <?php } $exhibitorCounter++; require( locate_template ('blocks/content-exhibitor.php')); echo $post->ID . '] ' . $post_type . ' - ' . get_permalink($post->ID) . "<BR>"; if ($exhibitorCounter == 2) {echo '</div>';} } if ($post_type == 'speakers') { if ($speakerCounter == 0) { echo '<div class="row"><div class="col-12 mb-2 mb-lg-3">'; ?> <span class="contentbrands__title type__weight--medium">Experts for <?php echo $contentbrand; ?>:</span> </div></div> <?php echo '<div class="row">'; ?> <?php } $speakerCounter++; require( locate_template ('blocks/content-speaker.php')); echo $post->ID . '] ' . $post_type . ' - ' . get_permalink($post->ID) . "<BR>"; if ($speakerCounter == 1) {echo '</div>';} } endwhile; // Previous/next page navigation. $args = array( 'prev_text' => __( 'Previous page', 'twentysixteen' ), 'next_text' => __( 'Next page', 'twentysixteen' ), 'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentysixteen' ) . ' </span>', 'screen_reader_text' => __( 'Posts navigation' ), 'type' => 'plain' ); // Set up paginated links. $links = paginate_links_with_provided_wpquery( $args , $loop); if ( $links ) { echo _navigation_markup( $links, 'pagination', $args['screen_reader_text'] ); } ?> <?php else : echo '<h3>No ss</h3>'; endif; wp_reset_postdata(); ?> business-leaders
Forum: Developing with WordPress
In reply to: Posts Per Page -> Per Post TypeHi @vrandom
Thanks once again for your reply.
I’m still getting the same error. Notice: Undefined index: post_type …
I have copied the class into functions.php and the other section into my page where I want to display the posts.
I have changed all the wp_ to my table prefix, including primary_wp, think this is right?
Also changed my post types in the array. I don’t know if I am missing something.
You should definitely make this into a plugin when it gets going!Code attached:
// ---------------------------------------------------- $post_slug='news'; //$post->post_name; $post_types = ['news', 'speakers', 'exhibitors']; // number of post to show per each post_type $post_per_posttype = 2; $sql_offset = get_query_var('paged', 0); if ($sql_offset -1 > 0) { $sql_offset = ($sql_offset - 1) * $post_per_posttype; } // Make SQL Parts $joinTemplate = []; $whereTemplate = []; $whereTemplateAddon = []; foreach ($post_types as $post_type_key => $post_type) { $joinTemplate[] = "left join (select GROUP_CONCAT(id order by post_date desc, id desc) as grouped_id, post_type from ( SELECT id, post_type, post_date FROM foe_342fj29x2_posts inner join foe_342fj29x2_term_relationships on foe_342fj29x2_term_relationships.object_id = id inner join foe_342fj29x2_term_taxonomy on foe_342fj29x2_term_relationships.term_taxonomy_id = foe_342fj29x2_term_taxonomy.term_taxonomy_id inner join foe_342fj29x2_terms on foe_342fj29x2_term_taxonomy.term_id = foe_342fj29x2_terms.term_id where foe_342fj29x2_term_taxonomy.taxonomy = 'category' and foe_342fj29x2_terms.slug = '$post_slug' and post_type in ('$post_type') and post_status = 'publish' order by post_date desc, id desc LIMIT 18446744073709551610 offset #sql_offset# ) d$post_type_key GROUP BY d$post_type_key.post_type) post_type_$post_type_key ON primary_foe_342fj29x2_posts.post_type = post_type_$post_type_key.post_type"; $whereTemplate[] = "primary_foe_342fj29x2_posts.post_type = post_type_$post_type_key.post_type"; $whereTemplateAddon[] = "AND (FIND_IN_SET(primary_foe_342fj29x2_posts.id, post_type_$post_type_key.grouped_id) <= $post_per_posttype and FIND_IN_SET(primary_foe_342fj29x2_posts.id, post_type_$post_type_key.grouped_id) > 0)"; } $sql_template = "select #sql_col# from foe_342fj29x2_posts primary_foe_342fj29x2_posts #join_templates_0# #join_templates_1# #join_templates_2# where ((#where_0# #where_addon_0#) or (#where_1# #where_addon_1#) or (#where_2# #where_addon_2#)) #append#"; // Assemble Queries $sqlQuerys['found_post_query'] = $sql_template; $sqlQuerys['wp_query'] = $sql_template; $found_posts_sql = $sql_template; foreach ($post_types as $post_type_key => $post_type) { $sqlQuerys['found_post_query'] = str_replace("#sql_col#", 'count(primary_foe_342fj29x2_posts.id)', $sqlQuerys['found_post_query']); $sqlQuerys['found_post_query'] = str_replace("#append#", 'group by primary_foe_342fj29x2_posts.post_type', $sqlQuerys['found_post_query']); $sqlQuerys['found_post_query'] = str_replace("#where_addon_$post_type_key#", '', $sqlQuerys['found_post_query']); $sqlQuerys['found_post_query'] = str_replace("#join_templates_$post_type_key#", str_replace("#sql_offset#", 0, $joinTemplate[$post_type_key]), $sqlQuerys['found_post_query']); $sqlQuerys['found_post_query'] = str_replace("#where_$post_type_key#", $whereTemplate[$post_type_key], $sqlQuerys['found_post_query']); $sqlQuerys['wp_query'] = str_replace("#sql_col#", '*', $sqlQuerys['wp_query']); $sqlQuerys['wp_query'] = str_replace("#append#", "", $sqlQuerys['wp_query']); $sqlQuerys['wp_query'] = str_replace("#where_addon_$post_type_key#", $whereTemplateAddon[$post_type_key], $sqlQuerys['wp_query']); $sqlQuerys['wp_query'] = str_replace("#join_templates_$post_type_key#", str_replace("#sql_offset#", $sql_offset, $joinTemplate[$post_type_key]), $sqlQuerys['wp_query']); $sqlQuerys['wp_query'] = str_replace("#where_$post_type_key#", $whereTemplate[$post_type_key], $sqlQuerys['wp_query']); } // global $wpdb; // need to pass the max posts possible to the query as it would not be generated correctly $page_count_per_post_type = $wpdb->get_results($sqlQuerys['found_post_query'], ARRAY_N); // get the largest page count on a page $largest_page_count_per_post_type = 0; $found_posts = 0; foreach ($page_count_per_post_type as $page_count) { $largest_page_count_per_post_type = ($page_count[0] > $largest_page_count_per_post_type)? $page_count[0]:$largest_page_count_per_post_type; $found_posts += $page_count[0]; } // page_per_posts is for pagination (post_per_posttype * num_of_posttypes_in_query) $loop = new WP_Query_CustomSQL($sqlQuerys['wp_query'], array( 'posts_per_page' => $post_per_posttype * 3 , 'found_posts' => $found_posts, 'max_num_pages' => ceil( $largest_page_count_per_post_type / $post_per_posttype ))); // put the post in the order of the post_types array if ($loop->have_posts()) { // make posts index by post_type $tmpPosts = []; foreach ($loop->posts as $k => $v) { $tmpPosts[get_post_type( $v->ID )][] = $v; } // assemble new ordered posts $finPosts = []; foreach ($post_types as $k => $v) { if (isset($tmpPosts[$v])) { foreach ($tmpPosts[$v] as $k1 => $v2) { $finPosts[] = $v2; } } } // update the $loop with the new ordered posts $loop->posts = $finPosts; } $speakerCounter = 0; $exhibitorCounter = 0; $columnwidth = 'col-lg-6'; if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); $post_type = get_post_type( $post->ID ); if ($post_type == 'post1' && $newsCounter < 2) { $newsCounter++; //require( locate_template ('blocks/content-newsrow.php')); echo $post->ID . '] ' . $post_type . ' - ' . get_permalink($post->ID) . "<BR>"; } if ($post_type == 'post2' && $exhibitorCounter < 2) { if ($exhibitorCounter == 0) { echo '<div class="row"><div class="col-12 mb-2 mb-lg-3">'; ?> <span class="contentbrands__title type__weight--medium">Exhibitors for <?php echo $contentbrand; ?>:</span> </div></div> <?php echo '<div class="row">'; ?> <?php } $exhibitorCounter++; //require( locate_template ('blocks/content-exhibitor.php')); echo $post->ID . '] ' . $post_type . ' - ' . get_permalink($post->ID) . "<BR>"; if ($exhibitorCounter == 2) {echo '</div>';} } if ($post_type == 'post3' && $speakerCounter < 2) { if ($speakerCounter == 0) { echo '<div class="row"><div class="col-12 mb-2 mb-lg-3">'; ?> <span class="contentbrands__title type__weight--medium">Experts for <?php echo $contentbrand; ?>:</span> </div></div> <?php echo '<div class="row">'; ?> <?php } $speakerCounter++; //require( locate_template ('blocks/content-speaker.php')); echo $post->ID . '] ' . $post_type . ' - ' . get_permalink($post->ID) . "<BR>"; if ($speakerCounter == 1) {echo '</div>';} } endwhile; // Previous/next page navigation. $args = array( 'prev_text' => __( 'Previous page', 'twentysixteen' ), 'next_text' => __( 'Next page', 'twentysixteen' ), 'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentysixteen' ) . ' </span>', 'screen_reader_text' => __( 'Posts navigation' ), 'type' => 'plain' ); // Set up paginated links. $links = paginate_links_with_provided_wpquery( $args , $loop); if ( $links ) { echo _navigation_markup( $links, 'pagination', $args['screen_reader_text'] ); } ?> <?php else : echo '<h3>No News</h3>'; endif; wp_reset_postdata(); ?>
Forum: Developing with WordPress
In reply to: Posts Per Page -> Per Post TypeUpon digging, when printing this $q variable, I get Array ( [posts_per_page] => 6 [found_posts] => 0 ).
Presuming it has something to do with the found posts.Forum: Developing with WordPress
In reply to: Posts Per Page -> Per Post TypeHi @vrandom
Wow, thank you VERY much for all of this, you have certainly gone to a lot of trouble to look at this for me.
I defintley wouldnt have the knowledge to come up with something like this.I have pasted your extended class in my functions.php and popped the sql code in my page-news.php.
After changing the table prefixes within the sql query, I am faced with this error:Notice
: Undefined index: post_type in
/wordpress/localhost/website/wp-content/themes/website_theme/functions.php
on line
1131Not sure where to go from here, thanks again.
// ---------------------------------------------------- $post_slug=$post->post_name; // number of post to show per each post_type $post_per_posttype = 2; // to query $sql ="SELECT foe_342fj29x2_posts.* FROM foe_342fj29x2_posts INNER JOIN ( SELECT GROUP_CONCAT(ID ORDER BY post_date DESC) grouped_id, post_type FROM foe_342fj29x2_posts inner join foe_342fj29x2_term_relationships on foe_342fj29x2_term_relationships.object_id = foe_342fj29x2_posts.id inner join foe_342fj29x2_term_taxonomy on foe_342fj29x2_term_relationships.term_taxonomy_id = foe_342fj29x2_term_taxonomy.term_taxonomy_id inner join foe_342fj29x2_terms on foe_342fj29x2_term_taxonomy.term_id = foe_342fj29x2_terms.term_id where foe_342fj29x2_term_taxonomy.taxonomy = 'category' and foe_342fj29x2_terms.slug = '$post_slug' and post_type in ('news', 'exhibitors', 'speakers') GROUP BY post_type ) group_max ON foe_342fj29x2_posts.post_type = group_max.post_type AND FIND_IN_SET(foe_342fj29x2_posts.id, grouped_id) <=#post_per_posttype# ORDER BY foe_342fj29x2_posts.id, post_type DESC LIMIT 18446744073709551610 OFFSET 0;"; global $wpdb; // need to pass the max posts possible to the query as it would not be generated correctly $found_posts = $wpdb->query(str_replace('#post_per_posttype#', 18446744073709551610, $sql)); // page_per_posts is for pagination (post_per_posttype * num_of_posttypes_in_query) $loop = new WP_Query_CustomSQL(str_replace('#post_per_posttype#', $post_per_posttype, $sql), array( 'posts_per_page' => $post_per_posttype * 3 , 'found_posts' => $found_posts)); $speakerCounter = 0; $exhibitorCounter = 0; $columnwidth = 'col-lg-6'; if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); $post_type = get_post_type( $post->ID ); if ($post_type == 'news' && $newsCounter < 2) { $newsCounter++; require( locate_template ('blocks/content-newsrow.php')); echo $post_type . ' - ' . get_permalink($post->ID) . "<BR>"; } if ($post_type == 'exhibitors' && $exhibitorCounter < 2) { if ($exhibitorCounter == 0) { echo '<div class="row"><div class="col-12 mb-2 mb-lg-3">'; ?> <span class="contentbrands__title type__weight--medium">Exhibitors for <?php echo $contentbrand; ?>:</span> </div></div> <?php echo '<div class="row">'; ?> <?php } $exhibitorCounter++; require( locate_template ('blocks/content-exhibitor.php')); //echo $post_type . ' - ' . get_permalink($post->ID) . "<BR>"; if ($exhibitorCounter == 2) {echo '</div>';} } if ($post_type == 'speakers' && $speakerCounter < 2) { if ($speakerCounter == 0) { echo '<div class="row"><div class="col-12 mb-2 mb-lg-3">'; ?> <span class="contentbrands__title type__weight--medium">Experts for <?php echo $contentbrand; ?>:</span> </div></div> <?php echo '<div class="row">'; ?> <?php } $speakerCounter++; require( locate_template ('blocks/content-speaker.php')); //echo $post_type . ' - ' . get_permalink($post->ID) . "<BR>"; if ($speakerCounter == 1) {echo '</div>';} } endwhile; // Previous/next page navigation. $args = array( 'prev_text' => __( 'Previous page', 'twentysixteen' ), 'next_text' => __( 'Next page', 'twentysixteen' ), 'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentysixteen' ) . ' </span>', 'screen_reader_text' => __( 'Posts navigation' ), 'type' => 'plain' ); // Set up paginated links. $links = paginate_links_with_provided_wpquery( $args , $loop); if ( $links ) { echo _navigation_markup( $links, 'pagination', $args['screen_reader_text'] ); } ?> <?php else : echo '<h3>No News</h3>'; endif; wp_reset_postdata(); ?>
- This reply was modified 6 years ago by Ainsley Clark.
Forum: Developing with WordPress
In reply to: Posts Per Page -> Per Post TypeHi @vrandom
Thanks again for your reply, I cant say that I am proficient with SQL so I only half got your reply.
I tried to use a counter for each post type, but I’m getting some funky results, some appearing on different pages etc.
Would be very appricaitie if you can help me out! Many thanks.
<?php $post_slug=$post->post_name; $args = array( 'post_type' => array('news', 'exhibitors', 'speakers'), 'posts_per_page' => 6, 'order' => 'DESC', 'orderby' => 'type', 'paged' => $currpage, 'tax_query' => array( array( 'taxonomy' => 'foebar', 'field' => 'slug', 'terms' => array( $post_slug ) ) )); $loop = new WP_Query( $args ); $speakerCounter = 0; $exhibitorCounter = 0; $newsCounter = 0; $columnwidth = 'col-lg-6'; if ( have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); $post_type = get_post_type( $post->ID ); if ($post_type == 'news' && $newsCounter < 2) { $newsCounter++; require( locate_template ('blocks/content-newsrow.php')); } if ($post_type == 'exhibitors' && $exhibitorCounter < 2) { if ($exhibitorCounter == 0) { echo '<div class="row"><div class="col-12 mb-2 mb-lg-3">'; ?> <span class="contentbrands__title type__weight--medium">Exhibitors for <?php echo $contentbrand; ?>:</span> </div></div> <?php echo '<div class="row">'; ?> <?php } $exhibitorCounter++; require( locate_template ('blocks/content-exhibitor.php')); if ($exhibitorCounter == 2) {echo '</div>';} } if ($post_type == 'speakers' && $speakerCounter < 2) { if ($speakerCounter == 0) { echo '<div class="row"><div class="col-12 mb-2 mb-lg-3">'; ?> <span class="contentbrands__title type__weight--medium">Experts for <?php echo $contentbrand; ?>:</span> </div></div> <?php echo '<div class="row">'; ?> <?php } $speakerCounter++; require( locate_template ('blocks/content-speaker.php')); if ($speakerCounter == 1) {echo '</div>';} } endwhile; else : echo '<h3>No News</h3>'; endif; wp_reset_postdata(); ?> </div> <div class="col-4"> <?php get_sidebar(); ?> </div> <div class="col-12 pagination"> <?php echo previous_posts_link( 'Previous Page', $loop->max_num_pages); echo get_next_posts_link( 'Next Page', $loop->max_num_pages ); ?> </div> </div><!-- End Row -->
Forum: Developing with WordPress
In reply to: Posts Per Page -> Per Post TypeHi @vrandom
Thank you very much for your quick reply.
Currently I am using WP Query to list the posts, I originally had these as different query’s but I thought I couldn’t use pagination for the page?
How would I display 2 posts of each custom post type using pagination as well? Code is below, thank you again.<?php $post_slug=$post->post_name; $args = array( 'post_type' => array('news', 'exhibitors', 'speakers'), 'posts_per_page' => 2, 'order' => 'DESC', 'orderby' => 'type', 'paged' => $paged, 'tax_query' => array( array( 'taxonomy' => 'foebar', 'field' => 'slug', 'terms' => array( $post_slug ) ) )); $loop = new WP_Query( $args ); $speakerCounter = 0; $exhibitorCounter = 0; $columnwidth = 'col-lg-6'; if ( have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); $post_type = get_post_type( $post->ID ); if ($post_type == 'news') { require( locate_template ('blocks/content-newsrow.php')); } if ($post_type == 'exhibitors') { if ($exhibitorCounter == 0) {echo '<div class="row">';} $exhibitorCounter++; echo $exhibitorCounter; require( locate_template ('blocks/content-exhibitor.php')); if ($exhibitorCounter == 1) {echo '</div>';} } if ($post_type == 'speakers') { if ($speakerCounter == 0) {echo '<div class="row">';} $speakerCounter++; require( locate_template ('blocks/content-speaker.php')); if ($speakerCounter == 1) {echo '</div>';} } endwhile; else : echo '<h3>No News</h3>'; endif; wp_reset_postdata(); ?>
Forum: Fixing WordPress
In reply to: Customiser Broken after Updating PermalinksAny ideas?