A
A
Alex-Broudy2018-09-05 22:51:59
WordPress
Alex-Broudy, 2018-09-05 22:51:59

How to display a list of pages with a custom field on a wordpress page with a specific page ID, the value of which is equal to the ID of the current page?

Greetings!
The page structure is as follows:
Parent: Credit Cards
Pages: Card #1, Card #2, Card #3, etc.
There are pages of banks, for example, Bank #1 has page ID = 124, and some credit cards from the list have an arbitrary field, the value of which is equal to the ID of this bank, and a list of these cards should be displayed on the page of this bank.
The bank page template has the following code:

<?php 
  $nomer = get_the_ID();
  $args = array(
    'post_type'    => 'page',
    'meta_query' => array(
      array(
      'key' => 'bank_id', 
      'value' => $nomer, 
    )
    ), 
    ); 
    $pages = get_pages( $args );
      foreach( $pages as $post ){
        setup_postdata( $post );
        ?> 
          <div><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
        <?php 
          }  
    wp_reset_postdata();
            
?>

the goal is to get the ID of the page of the bank where we are, and display a list of cards with an arbitrary field corresponding to the ID of a specific page.
But nothing comes out - a list of all possible pages of the site is displayed.
Tell me what I'm doing wrong, why it doesn't work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex-Broudy, 2018-09-05
@Alex-Broudy

Found a solution here :

<?php 
$nomer = get_the_ID(); // Определяем ID страницы на которой находимся
// Задаем параметры цикла:
$args = array(
  'numberposts'	=> -1,
  'post_type'		=> 'page',
  'meta_key'		=> 'bank_id',
  'meta_value'	=> $nomer
);

// С помощью WP_Query создаем переменную, содержащую все страницы со значением ЗАО в поле округ
$the_query = new WP_Query( $args );

?>

<?php if( $the_query->have_posts() ): ?>
  <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <div><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
  <?php endwhile; ?>
<?php endif; ?>

<?php wp_reset_query();	 // Возвращаем в норму все глобальные переменные ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question