• External11

    (@external11)


    Hello,

    I am trying to list one page (the recent one) which has the same TITLE like the current post META VALUE.

    I have come up with the following code, however it is not showing the correct page. Can you please help?

    <?php
    	   $pagetitle = get_post_meta($post->ID, 'car_model_range', TRUE);
         $args=array(
         'post_type' => 'page',
         'post_status' => 'publish',
         'post_title' => $pagetitle,
         'order'=>'ASC',
         'orderby'=>'date',
         'posts_per_page' => 1,
    	   );
    	$my_query = new WP_Query($args);
    	if( $my_query->have_posts() ) {
    	  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <?php get_template_part( 'partials/caritem', 'page' ); ?>
    	    <?php
    	  endwhile;
    	}
    ?>

    Example: there is a page with title “The best page ever” and then I have a post with key car_model_range = “The best page ever”. I want to see on that post also the page that has this title.

Viewing 6 replies - 1 through 6 (of 6 total)
  • vtxyzzy

    (@vtxyzzy)

    ‘post_title’ is not a valid argument to WP_Query. Try changing this:

    'post_title' => $pagetitle,

    to this:

    'name' => sanitize_title($pagetitle),
    Thread Starter External11

    (@external11)

    I tried this but it is showing no results:

    <?php
    	   $pagetitle = get_post_meta($post->ID, 'car_model_range', TRUE);
         $args=array(
         'post_type' => 'page',
         'post_status' => 'publish',
         'pagename' => sanitize_title($pagetitle),
         'order'=>'ASC',
         'orderby'=>'date',
         'posts_per_page' => 1,
    	   );
    	$my_query = new WP_Query($args);
    	if( $my_query->have_posts() ) {
    	  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <?php get_template_part( 'partials/caritem', 'page' ); ?>
    	    <?php
    	  endwhile;
    	}
    ?>
    vtxyzzy

    (@vtxyzzy)

    The argument is not ‘pagename’, just ‘name’.

    'name' => sanitize_title($pagetitle),
    Thread Starter External11

    (@external11)

    none of the codes is working.

    By the way here is the difference:

    name (string) – use post slug.
    pagename (string) – use page slug.

    maybe it is necessary to sanitize the title already on he first row.

    Michael

    (@alchymyth)

    Thread Starter External11

    (@external11)

    Hi Alchymyth,

    This function retrieves a post given its title. If more that one post uses the same title, the post with the smallest ID will be returned.

    However I need to ‘order’=>’ASC’, ‘orderby’=>’date’.

    It seems like to solution is not that easy…

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘List a page which has the same title like current post's meta value’ is closed to new replies.