【问题标题】:WordPress - request post by url with custom field valueWordPress - 通过带有自定义字段值的 url 请求发布
【发布时间】:2016-11-11 11:11:25
【问题描述】:

我有一个这样的网址: example.com/movies/157336/Interstellar

供参考: example.com/movies/%movie_id%/%movie_name%

请注意,movie_id 是一个自定义字段,而不是实际的帖子 ID。 movie_name 是帖子标题,但仅出于 SEO 原因。

我需要WordPress根据URL中找到的自定义字段movie_id加载页面,而不使用页面名称,如果没有找到movie_id,则抛出常规404错误。

我遇到的主要问题是,我似乎无法让 WordPress 根据 URL 中的自定义字段 movie_id 加载页面,它总是使用 movie_name 作为参考点。

我怎么知道?那么正确的 URL 应该是 example.com/movies/157336/Interstellar,如果我将标题更改为 example.com/movies/157336/Intersterlarxyz,那么 WordPress 会给出 404 错误。 如果我更改了 ID 但保留了正确的电影名称,例如:example.com/movies/123/Interstellar,那么 WordPress 仍然会加载正确的页面。

基于这种行为,可以肯定地说 WordPress 基于 URL 中的页面 slug 加载页面,而不是电影 ID,这就是我需要解决的问题。

到目前为止,这是我的代码:

电影插件.php

// Register Custom Post Type "movies"
function register_moviedb_post_type() {
  register_post_type( 'movies',
    array(
            'labels' => array(
                'name' => __( 'Movies' ),
                'singular_name' => __( 'Movies' )
            ),
            'taxonomies' => array('category'), 
            'public' => true,
            'has_archive' => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'query_var' => true,
            'rewrite' => array('slug' => 'movies','with_front' => FALSE),
            'supports' => array( 'title', 'editor', 'custom-fields','comments','page-attributes','trackbacks','revisions', 'thumbnail')
    )
  );
  flush_rewrite_rules();
}
add_action( 'init', 'register_moviedb_post_type' );


// Add custom rewrite tag 'movie_id'
function custom_rewrite_tag() {
  add_rewrite_tag('%movie_id%', '([^/]+)', 'movie_id=');
}
add_action('init', 'custom_rewrite_tag');


// Add rewrite rule
function custom_rewrite_basic() {
  add_rewrite_rule(
            '^movies/([^/]*)/([^/]*)/?',
            //'index.php?post_type=movies&movie_id=$matches[1]',
            'index.php?post_type=movies&movie_id=$matches[1]&name=$matches[2]',
            'top'
        );
}
add_action('init', 'custom_rewrite_basic');


// Query var 'movie_id'
function add_query_vars_filter( $vars ){
  $vars[] = "movie_id";
  return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );


// Custom Page Template
function custom_movie_page_template() {
    if ( is_singular( 'movies' ) ) {
        add_filter( 'template_include', function() {
            return plugin_dir_path( __FILE__ ) . '/movies.php';
        });
    }
}
add_action( 'template_redirect', 'custom_movie_page_template' );

// Create custom post type link for movies
function movie_post_type_link( $link, $post = 0 ){
    if ( $post->post_type == 'movies' ){

            $id = $post->ID;
            $post = &get_post($id);
            $movie_id = get_post_meta($post->ID,'movie_id', true);

            empty ( $post->slug )
            and $post->slug = sanitize_title_with_dashes( $post->post_title );
            return home_url(
           user_trailingslashit( "movies/$movie_id/$post->slug" )
        );
    } else {
        return $link;
    }
}
add_filter( 'post_type_link', movie_post_type_link, 1, 2 );

如果我从 add_rewrite_rule 中的 URL 中删除 movie_name,WordPress 只会加载该帖子类型的存档页面。 'index.php?post_type=movies&movie_id=$matches[1]',

如果我在 URL 重写中使用页面名称,它总是根据名称而不是 movie_id 加载页面。 'index.php?post_type=movies&movie_id=$matches[1]&name=$matches[2]',

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我没有测试下面的代码,但是movies.php可能是这样的:

    <?php $movieId = (int)get_query_var('movie_id') ;
    if(movieId ==! 0):
    
        $args = array(
            'post_type' => 'movies',
            'posts_per_page' => 1,
            'meta_key'      => 'movie_id',
            'meta_value'    => $movieId
        );
    
        $the_query = new WP_Query( $args );
    
        if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        //code that display data
        <?php endwhile; else : ?>
        //code to create post
        <?php endif;
    else:
    //normal loop
    endif;
    

    【讨论】:

    • 我确实有一个movie.php 模板文件,它看起来和你的很相似,但这不是问题所在。问题是,WordPress 总是使用 URL 中的页面名称来确定要加载哪个页面,而不是使用 movie_id。 movie.php 文件中的自定义 wp_query 不会改变这种行为。站点标题和其他相关内容仍将是基于 URL 中页面名称的页面内容。如果我从 URL 中删除页面名称,那么 wordpress 只会加载 archive.php 文件。
    猜你喜欢
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    相关资源
    最近更新 更多