WP Query get和id by $ _GET并将其更改为CPT链接

时间:2018-05-21 07:33:49

标签: php wordpress

我正在尝试进行查询以获取ID并返回cpt自定义链接。这是我的代码,但无法结束:

<?php
require_once('wp-load.php');

    $args = array(
        'post_type' =>  'catalogos',
        'post_status' => 'publish',
        'id' => $_GET['id'],
    );

    $query= new WP_Query($args);

    $link = get_field('link');

    if(isset($_GET['id'])){
        wp_redirect(home_url()"/".$link."/");
    }



?>

2 个答案:

答案 0 :(得分:1)

http://yoursite.com/?id=your_page_id eg:2

 $args = array(
                'post_type' =>  'catalogos',
                'post_status' => 'publish',
                'id' => $_GET['id'],
            );

            $query= new WP_Query($args);

            if ( $query->have_posts() ) {
            while ( $query->have_posts() ){
              $query->the_post();
            $post_id = get_the_ID();
               $link = get_field('link');
             if($link){
                wp_redirect(home_url()"/".$link."/?id=".$post_id);
            }
            }
          }
          wp_reset_postdata();

答案 1 :(得分:0)