Home > database >  WordPress how to filter what pages to display, if page URL/slug contains -nv/
WordPress how to filter what pages to display, if page URL/slug contains -nv/

Time:11-13

WordPress how to filter what pages to display, if page URL/slug contains -nv/

now it shows all the pages on the website, the question is how to filter this to show only pages that contain -nv/ this part URL is unique and matches all the page what i need to display

?><style>
table {
    counter-reset: rowNumber;
}

table tr:not(:first-child) {
    counter-increment: rowNumber;
}

table tr td:first-child::before {
    content: counter(rowNumber);
    min-width: 1em;
    margin-right: 0.5em;
    color: #cfcfcf;
}</style>
    <table>
  <tr>
    <th>id</th>
    <th>title</th>
    <th>url</th>
  </tr>
<?php // Query for listing all pages in the select box loop
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query( array(
'post_type' => 'page',
'posts_per_page' => -1,
 'orderby' => 'title',
 'order'   => 'ASC', // or DESC
));

foreach ($all_wp_pages as $value){
$post = get_page($value);
$title = $post->post_title;
$id = $post->ID;

?> 
  <tr>
  <?php  echo '<td>' . $id. '</td>
    <td>' . $title . '</td>
    <td>';
        global $post;
  if ( $post) { ?>
    <?php echo get_permalink( $id ); ?></td>
  
<?php }
    
}; ?>
</tr>
    </table>

thank you in advance, for being so helpful.

CodePudding user response:

Check if post slug contains -nv using str_contains() function.

Try out this code in foreach loop under $id.

$slug = $post->post_name;

if (!str_contains($slug, '-nv')) {
    continue;
}
  • Related