Support » Plugin: WP Post Nav » exclude certain pages when navigating pages

  • Hello, first of all thank you for this plugin. Great job. I use this plugin to switch between pages. Is it possible to exclude the pages I have specified from this navigation?
    good work

Viewing 15 replies - 1 through 15 (of 37 total)
  • Plugin Author Jo4nny8

    (@jo4nny8)

    @petraagency Thats not currently possible.

    It might be feasible by modifying the where clause in the next and previous posts.

    This code is untested!

    
    add_filter( 'get_previous_post_where', 'wppnav_exclude_posts' );
    add_filter( 'get_next_post_where', 'wppnav_exclude_posts' );
    function wppnav_exclude_posts( $where ) {
        return $where . ' AND p.ID NOT IN (1433, 1344)';
    }
    

    1433 and 1344 are post ids so you need to alter these to suit the page ids youre excluding.

    Also, as its untested, im not sure what effect this would have on the current where clause I use for excluding out of stock products with the WooCommerce option so you might want to wrap that filter inside a conditional that only runs on pages.

    Thread Starter petraagency

    (@petraagency)

    Hello there,
    Thank you for your answer, I copied your code into theme functions.php and edited the corresponding ID’s but it didn’t work. do i need to add it with some other code?

    Thread Starter petraagency

    (@petraagency)

    Now I realized you have edited the code as a post, but the part I tried to exclude is pages.

    • This reply was modified 1 year, 6 months ago by petraagency.
    Plugin Author Jo4nny8

    (@jo4nny8)

    Can you post your code here?

    All you need are the page ids which you can get by editing the page in wp-admin and click the browser url to get the is of the page as opposed to the name

    Thread Starter petraagency

    (@petraagency)

    add_filter( ‘get_previous_post_where’, ‘wppnav_exclude_posts’ );
    add_filter( ‘get_next_post_where’, ‘wppnav_exclude_posts’ );
    function wppnav_exclude_posts( $where ) {
    return $where . ‘ AND p.ID NOT IN (742, 744)’;
    }

    742 -> aboutus
    744 -> contact

    Thread Starter petraagency

    (@petraagency)

    add_filter( 'get_previous_post_where', 'wppnav_exclude_posts' );
    add_filter( 'get_next_post_where', 'wppnav_exclude_posts' );
    function wppnav_exclude_posts( $where ) {
        return $where . ' AND p.ID NOT IN (742, 744)';
    }
    Plugin Author Jo4nny8

    (@jo4nny8)

    If you look at the function for excluding out of stock products it uses a join query in the where so you may need the same.

    Taken from class-wp-post-nav-public.php
    `
    468 public function wppostnav_outofstock($where) {
    469 global $wpdb;
    470 return $where . ” AND p.ID IN (
    471 SELECT p.ID FROM $wpdb->posts p
    472 LEFT JOIN $wpdb->postmeta m ON p.ID = m.post_id
    473 WHERE m.meta_key = ‘_stock_status’
    474 AND m.meta_value = ‘instock’ )”;
    475 }

    Thread Starter petraagency

    (@petraagency)

    Hi
    I have almost no php knowledge, can you help me and how to write this function?

    Plugin Author Jo4nny8

    (@jo4nny8)

    @petraagency

    Ok I’ll take a look and let you know when I’ve sorted it for you

    Thread Starter petraagency

    (@petraagency)

    Thank you

    Thread Starter petraagency

    (@petraagency)

    Hi
    Did you have a chance to try it?

    Plugin Author Jo4nny8

    (@jo4nny8)

    @petraagency unfortunately I haven’t had chance yet. It’s likely to be a couple of days before I do

    Plugin Author Jo4nny8

    (@jo4nny8)

    @petraagency

    Managed to grab some time. please try this. Just alter the numbers just before the closing bracket to suit your pages to exclude (13,112)

    
    function wppostnav_exclude_pages($where) {
        global $wpdb;
        var_dump ('hello' . $where);
        return $where . " AND p.ID IN ( 
                          SELECT p.ID FROM $wpdb->posts p 
                          LEFT JOIN $wpdb->postmeta m ON p.ID = m.post_id 
                          WHERE p.ID != '13,112')";
      }
    add_filter( 'get_next_post_where', 'wppostnav_exclude_pages',10,1);
    
    Plugin Author Jo4nny8

    (@jo4nny8)

    bugger remove the var_dump (hello) bit as I was testing and forgot to remove it

    Thread Starter petraagency

    (@petraagency)

    hi,
    First of all, sorry for the late conversion, but I was able to get to my computer. I tried correcting the code you gave as follows, but nothing changed, is there a place where I did it wrong?

    function wppostnav_exclude_pages($where) {
        global $wpdb;
        return $where . " AND p.ID IN ( 
                          SELECT p.ID FROM $wpdb->posts p 
                          LEFT JOIN $wpdb->postmeta m ON p.ID = m.post_id 
                          WHERE p.ID != '742, 744')";
      }
    add_filter( 'get_next_post_where', 'wppostnav_exclude_pages',10,1);
    • This reply was modified 1 year, 6 months ago by petraagency.
Viewing 15 replies - 1 through 15 (of 37 total)
  • The topic ‘exclude certain pages when navigating pages’ is closed to new replies.