Forum Replies Created

Viewing 15 replies - 16 through 30 (of 46 total)
  • Thread Starter Ainsley Clark

    (@ainsleyclark)

    Hi @vrandom

    Thank you for your continued support on this.

    I have copied the gist over but nothing returned, I dumped the results in the <pre> tags and got this, not sure if the null has something to do with it?

    array(3) {
      [0]=>
      array(3) {
        [0]=>
        string(3) "102"
        [1]=>
        string(10) "exhibitors"
        [2]=>
        NULL
      }
      [1]=>
      array(3) {
        [0]=>
        string(2) "32"
        [1]=>
        string(4) "news"
        [2]=>
        NULL
      }
      [2]=>
      array(3) {
        [0]=>
        string(2) "31"
        [1]=>
        string(8) "speakers"
        [2]=>
        NULL
      }
    }
    • This reply was modified 5 years, 12 months ago by Ainsley Clark.
    Thread Starter Ainsley Clark

    (@ainsleyclark)

    Hi @vrandom

    School boy error, apologies!

    Have updated the code, wrapped the if and while within the main loop is that correct?
    Its not returning anything ‘No News’

    Updated gist:

    https://gist.github.com/ainsleyclark/aa719471b72246215cd725443dcde9b5

    Thread Starter Ainsley Clark

    (@ainsleyclark)

    Thread Starter Ainsley Clark

    (@ainsleyclark)

    Hi @vrandom
    Getting quite a few errors with this new code:

    Warning: min(): Array must contain at least one element in /Users/Ainsley/Dropbox/Wordpress/localhost/website/wp-content/themes/Website/page-contentbrands.php on line 219

    Notice: Undefined offset: 0 in /Users/Wordpress/localhost/website/wp-content/themes/Website/page-contentbrands.php on line 222

    Notice: Undefined offset: 0 in /Users/Wordpress/localhost/website/wp-content/themes/Website/page-contentbrands.php on line 222

    Notice: Undefined offset: 0 in /Users/Wordpress/localhost/website/wp-content/themes/Website/page-contentbrands.php on line 222

    Warning: array_slice() expects parameter 1 to be array, null given in /Users/Wordpress/localhost/website/wp-content/themes/Website/page-contentbrands.php on line 222

    Notice: Undefined offset: 0 in /Users/Wordpress/localhost/website/wp-content/themes/Website/page-contentbrands.php on line 225

    Warning: count(): Parameter must be an array or an object that implements Countable in /Users/Wordpress/localhost/website/wp-content/themes/Website/page-contentbrands.php on line 225

    Notice: Undefined offset: 0 in /Users/Wordpress/localhost/website/wp-content/themes/Website/page-contentbrands.php on line 225

    Notice: Undefined offset: 0 in /Users/Wordpress/localhost/website/wp-content/themes/Website/page-contentbrands.php on line 225

    Warning: count(): Parameter must be an array or an object that implements Countable in /Users/Wordpress/localhost/website/wp-content/themes/Website/page-contentbrands.php on line 229

    Warning: array_combine() expects parameter 1 to be array, null given in /Users/Wordpress/localhost/website/wp-content/themes/Website/page-contentbrands.php on line 229

    Thread Starter Ainsley Clark

    (@ainsleyclark)

    Thank you very much @vrandom for all of this. Will take a look tomorrow.

    Thread Starter Ainsley Clark

    (@ainsleyclark)

    @vrandom Amazing amazing.

    So the only thing is its returning 1 more page than it should.
    Can I do a -1 somewhere?! I know its probably not that simple!

    Also, if this is a complete pain – dont worry. But if there is say 10 news stories and only 2 speakers and 2 exhibitors, it would create 5 pages.
    Is there a way to populate that second page with news stories? So in turn it would create 3 pages.

    Page 1
    2 News
    2 Speakers
    2 Exhibitors

    Page 2
    6 Exhibitors

    Page 3
    2 News

    Thank you so much!

    Thread Starter Ainsley Clark

    (@ainsleyclark)

    Hi @vrandom

    It does make a lot of sense.

    With the code above, where shall I put this under paginate_links_with_provided_wpquery? Not entirely sure…

    Thread Starter Ainsley Clark

    (@ainsleyclark)

    @vrandom You absolute legend,

    Thank you so much! Apologies if I wasn’t clear.

    All working now.

    Have you any suggestions on the pagination? Just need the prev and next page, with the correct amount of pages.

    Can we take:

      array(3) {
        [0]=>
        string(2) "31"
        [1]=>
        string(4) "news"
        [2]=>
        string(15) "398,397,392,343"
      }

    And count of these and manually pass it into where the pagination is counted?

    Thread Starter Ainsley Clark

    (@ainsleyclark)

    @vrandom

    Ive spotted two problems:
    The last post is always for the very last post, regardless of the page.
    The post types arent in order. For example:

    array(3) {
      [0]=>
      array(3) {
        [0]=>
        string(2) "69"
        [1]=>
        string(10) "exhibitors"
        [2]=>
        string(10) "175,121,52"
      }
      [1]=>
      array(3) {
        [0]=>
        string(2) "31"
        [1]=>
        string(4) "news"
        [2]=>
        string(15) "398,397,392,343"
      }
      [2]=>
      array(3) {
        [0]=>
        string(2) "30"
        [1]=>
        string(8) "speakers"
        [2]=>
        string(2) "32"
      }
    }
    array(3) { ["exhibitors"]=> int(52) ["news"]=> int(343) ["speakers"]=> int(32) }

    Spits out
    1: Post ID – 397
    2: Post ID – 398
    And on the second page:
    3: Post ID – 343
    4: Post ID – 392

    On every page this:

    if (isset($last_post_id_by_type[$post_type])) {
       echo "<h1>{$last_post_id_by_type[$post_type]}</h1>";
    }

    Returns 343 (The last in the array)

    Which Im presuming why the one speaker works because there’s only one of them.

    Is there any work around with the pagination too?

    Thank you for your patience, most would have given up by now!

    Thread Starter Ainsley Clark

    (@ainsleyclark)

    @vrandom
    The last post count works if there’s only one post type in that page.

    Thread Starter Ainsley Clark

    (@ainsleyclark)

    Hi @vrandom

    I have used the var_dump to get the results, its below. This is for a total of 8, 4 news, 3 exhibitors and 1 speaker.

    array(3) {
      [0]=>
      array(3) {
        [0]=>
        string(2) "69"
        [1]=>
        string(10) "exhibitors"
        [2]=>
        string(10) "175,121,52"
      }
      [1]=>
      array(3) {
        [0]=>
        string(2) "31"
        [1]=>
        string(4) "news"
        [2]=>
        string(15) "398,397,392,343"
      }
      [2]=>
      array(3) {
        [0]=>
        string(2) "30"
        [1]=>
        string(8) "speakers"
        [2]=>
        string(2) "32"
      }
    }

    With regards to the last post count per page (of post type). I have popped your code in, but only works on some of the post types. I have printed the page_count array in the for loop and it looks liek the page number has something to do with it.
    I have included it below:

    // get the largest page count on a page
    $largest_page_count_per_post_type = 0;
    $found_posts = 0;
    $last_post_id_by_type= [];
    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];
    
       // get last post id for each type
        $tmp = explode(',', $page_count[2]);
        $last_post_id_by_type[$page_count[1]] = (int) end($tmp);
    
        echo '<h1>'.  print_r($page_count) . '</h1>';
    }

    SQL Querys:

        $sqlQuerys['found_post_query'] = str_replace("#sql_col#", 'count(foe_342fj29x2_posts.id), foe_342fj29x2_posts.post_type,'.$found_post_query_select_addon, $sqlQuerys['found_post_query']);
        //$sqlQuerys['found_post_query'] = str_replace("#sql_col#", 'count(foe_342fj29x2_posts.id),foe_342fj29x2_posts.post_type', $sqlQuerys['found_post_query']);
        $sqlQuerys['found_post_query'] = str_replace("#append#", 'group by 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']);

    Assemble Queries:

    // Assemble Queries
    $sqlQuerys['found_post_query'] = $sql_template;
    $sqlQuerys['wp_query'] = $sql_template;
    $found_post_query_select_addon = "CASE " . implode(' ', $selectFoundPostQueryAddon). " ELSE null END as post_ids";

    Addon:

    $selectFoundPostQueryAddon[] = "WHEN foe_342fj29x2_posts.post_type = '$post_type' THEN post_type_$post_type_key.grouped_id";

    Conditional:

                echo '<h1>' .$post->ID. '</h1>';
                if (isset($last_post_id_by_type[$post_type]) && $last_post_id_by_type[$post_type] == $post->ID) {
                    echo "<h1>Last Post</h1>";
                }
    Thread Starter Ainsley Clark

    (@ainsleyclark)

    Hi @vrandom .
    Thanks for the reply, the isset stuff worked.

    Hmmm, currently is resulting in 130 posts, when there is . total of 8 posts, 3 news, 3 exhibitors and 1 speaker.

    Would it be here?

    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];
    }

    On my other pages I only have prev page and next page, I was thinking loosing the 1, 2, 3, 4 etc would help but I dont think it would?

    Also is there a way of finding out the last post of that particular post type?

    E.g

    if ($post_type == 'news') {
        if ('this is the last post type of news') {
              //do something
        }
    }

    Thanks once again.

    Thread Starter Ainsley Clark

    (@ainsleyclark)

    Im getting the posts ?? My issue was with the conditionals within the loop.

    Is there a way to use isset so I can silence the error?

     if ( $q['cache_results'] )
                    update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache'])

    ;

    Other thing is that the pagination thinks it has 35 pages when it only has 2.

    Amazing. Thank you so much.

    Thread Starter Ainsley Clark

    (@ainsleyclark)

    Hi @vrandom

    Just to be sure, the first block I mentioned in my last post was just for testing right?

    I have removed primary_ from everything, I cant be sure what is used for?
    Anyway getting this error, but can see the pagination again.

    Notice: Undefined index: cache_results in

    
    $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 = '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 foe_342fj29x2_posts.post_type = post_type_$post_type_key.post_type";
    
            $whereTemplate[] = "foe_342fj29x2_posts.post_type = post_type_$post_type_key.post_type";
    
            $whereTemplateAddon[] = "AND (FIND_IN_SET(foe_342fj29x2_posts.id, post_type_$post_type_key.grouped_id) <= $post_per_posttype and FIND_IN_SET(foe_342fj29x2_posts.id, post_type_$post_type_key.grouped_id) > 0)";
    }
    
    $sql_template = "select #sql_col# from 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(foe_342fj29x2_posts.id)', $sqlQuerys['found_post_query']);
        $sqlQuerys['found_post_query'] = str_replace("#append#", 'group by 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']);
    }
    //
    Thread Starter Ainsley Clark

    (@ainsleyclark)

    And also WordPress database error: [Unknown column ‘primary_foe_342fj29x2_posts.post_type’ in ‘where clause’]

    Been through it a few times and the sql query thats spitting out results and dumping them:

    $sql = "select * from foe_342fj29x2_posts primary_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 = '$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) post_type_0
                ON primary_foe_342fj29x2_posts.post_type = post_type_0.post_type
    
                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)))";
    		
    $results = $wpdb->get_results($sql);
    
    echo "<pre>";
    var_dump($results);
    echo "</pre>";

    Is (I think the same as this) bar the variables.

    $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 = '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)";
    }
    
    $sql_template = "select #sql_col# from 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']);
    }
Viewing 15 replies - 16 through 30 (of 46 total)