InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Wordpress customization tutorials

  • Handle pagination in wordpress custom php code
  • How to and why disable wordpress cron
  • How to customize wordpress image alt tag
  • How to display date in wordpress pages
  • How to display wordpress page list with specific custom field value
  • How to display wordpress top level pages
  • How to hide a post from home and RSS feed in wordpress
  • How to include bootstrap javascript and css in wordpress post
  • How to include wordpress pages in archives
  • How to override priority and change frequency in Yoast xml sitemap
  • How to remove xmlrpc from wordpress headers
  • How to setup wordpress custom query params with pretty url format
  • How to show wordpress pages on front page with skip_home custom field
  • How to use google custom search for wordpress site
  • How to write custom php in wordpress
  • No frills social share links for Wordpress sites
  • Using Wordpress custom field for displaying code
  • Wordpress - add content filter after shortcode execution
  • Wordpress - customize category and tag links
  • Wordpress - customize facebook plugin opengraph meta tags
  • Wordpress - customize posts per page for tag, category and date archive pages
  • Wordpress - exclude specific posts from archive pages
  • Wordpress - get posts/pages with missing meta key
  • Wordpress - how to add filter to description meta tag
  • Wordpress - how to create custom tag cloud
  • Wordpress - how to exclude specific tag posts from a tag archive page
  • Wordpress - write custom php log to separate file
  • Wordpress how to check if a post is being viewed by admin
 
  • Home
  • > Tutorials
  • > Wordpress
  • > Wordpress customization

How to display wordpress page list with specific custom field value

By admin | Last updated on Mar 20, 2016

WordPress lets you display a list of pages as links with many customizations. You can use wp_list_pages or get_pages to display or get list of pages. These functions let you do couple of customization. Some of the main customizations are:

  1. Only list children of a specific page.
  2. Limit the page depth while displaying.
  3. Exclude some specific pages.
  4. Include some pages (this does not work with child_of argument though)

Since you can’t have tags in wordpress page, it does not let you do any customization based on tags which is a convenient feature for wordpress posts. Here is a workaround which can be implemented using custom fields. The main steps are:

  1. Create custom a field e.g. mypagetag or something similar. You can add one or more custom field (mypagetag) values for desired wordpress pages.
  2. Now a regular code to display pages will look like this:
    $parent = "PARENT_PAGE_ID";
    $args = array(
      'depth'        => 0,
      'show_date'    => '',
      'date_format'  => get_option('date_format'),
      'child_of'     => $parent,
      'exclude'      => '',
      'include'      => '',
      'title_li'     => __(''),
      'echo'         => 1,
      'authors'      => '',
      'sort_column'  => 'menu_order, post_title',
      'link_before'  => '',
      'link_after'   => '',
      'walker'       => '',
      'post_type'    => 'page',
      'post_status'  => 'publish'
    );
    $pages = get_pages($args);
    
    foreach ($pages as $page) {
      echo "<a href=\"" . get_page_link($page->ID) . "\">" . $page->post_title . "</a><br/>";
    }
    

    This code display all pages which are children of a specific page.
  3. To display pages with custom field mypagetag having a specific value (say mycustomvalue).
    $post_has_tag = array();
    $q = "SELECT post_id, meta_key, meta_value from wp_postmeta where meta_key='mypagetag' and meta_value='mycustomvalue'";
    $res_arr = $wpdb->get_results($q);
    foreach ($res_arr as $one) {
      $post_has_tag[$one->post_id] = 1;
    }
    
    foreach ($pages as $page) {
      if (array_key_exists($page->ID, $post_has_tag)) {
        echo "<a href=\"" . get_page_link($page->ID) . "\">" . $page->post_title . "</a><br/>";
      }
    }
    

    Here we could have got the custom field values per page also instead of querying table wp_postmeta. I did it this way to reduce the number of queries. wp_postmeta table has index on custom field (meta_key column name). Also I did not anticipate too many entries for a custom value mycustomvalue. So data returned is also not so large. If that is not the case for some specific situation, a different strategy can be taken.

Suggested posts:

  1. Mysql query to find all yoast meta description
  2. How to show wordpress pages on front page with skip_home custom field
  3. WordPress mysql query to get all posts with a specific custom field
  4. Using WordPress custom field for displaying code
  5. WordPress – how to exclude specific tag posts from a tag archive page
  6. Find all user created wordpress custom field keys
  7. WordPress mysql query to get all posts with a missing custom field
  8. find if a wordpress page is leaf page
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Tutorials, Webmaster, Wordpress, Wordpress customization

Follow InfoHeap

facebook
twitter
googleplus
  • Browse site
  • Article Topics
  • Article archives
  • Recent Articles
  • Contact Us
  • Omoney
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

Copyright © 2023 InfoHeap.

Powered by WordPress