Forums

[resolved] include pages when meta_value = page title? (4 posts)

  1. jamodu
    Member
    Posted 2 years ago #

    Hi,

    I'm trying to create a 'related pages' navigation and I need to include pages that have a custom field value equal to the page title.

    I've used this code, but it doesn't work properly - please can someone help?

    <?php
    global $post;
    $args=array(
      'post_type' => 'page',
      'meta_key' => 'Project Group',
      'meta_compare' => '=',
      'meta_value' => 'the_title();'
    );
    $pages = get_posts($args);
    if ($pages) {
      $pageids = array();
      foreach ($pages as $page) {
        $pageids[]= $page->ID;
      }
     ?>
      <ul>
       <?php wp_list_pages('include='.implode(",", $pageids)); ?>
    </ul>
    <?php
    }
    ?>
  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    Try:

    $this_page_title = the_title();
    args=array(
      'post_type' => 'page',
      'meta_key' => 'Project Group',
      'meta_value' => $this_page_title
    );
  3. jamodu
    Member
    Posted 2 years ago #

    Thanks for the reply esmi but I'm afraid that still doesn't work :(

    $this_page_title = the_title();
    @args=array(
      'post_type' => 'page',
      'meta_key' => 'Project Group',
      'meta_value' => $this_page_title
    );
  4. jamodu
    Member
    Posted 2 years ago #

    Finally sorted. It needed to be get_the_title rather than just the_title...

    <?php
    $this_page_title = get_the_title();
    global $post;
    $args=array(
      'post_type' => 'page',
      'meta_key' => 'Project Group',
      'meta_compare' => '=',
      'meta_value' => $this_page_title
    );
    
    $pages = get_posts($args);
    if ($pages) {
      $pageids = array();
      foreach ($pages as $page) {
        $pageids[]= $page->ID;
      }
     ?>
      <ul>
       <?php wp_list_pages('include='.implode(",", $pageids)); ?>
    </ul>
    <?php
    }
    ?>

    Hope that helps someone else in the future.

Topic Closed

This topic has been closed to new replies.

About this Topic