WordPress - 通过带有自定义字段值的 url 请求发布 [英] WordPress - request post by url with custom field value

查看:28
本文介绍了WordPress - 通过带有自定义字段值的 url 请求发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an URL like this: example.com/movies/157336/Interstellar

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

For reference: example.com/movies/%movie_id%/%movie_name%

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

Please note, the movie_id is a custom field, and not the actual post ID. The movie_name is the post title slug, but is only there for SEO reasons.

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

I need WordPress to load the page based on the custom field movie_id found in the URL and not use the page name, and if the movie_id is not found, throw the regular 404 error.

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

The main problem I am having, is that I can’t seem to get WordPress to load a page based on the custom field movie_id from the URL, it always uses movie_name as reference point.

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

How do I know that? Well the correct URL would be example.com/movies/157336/Interstellar and if I change the title to example.com/movies/157336/Intersterlarxyz then WordPress gives a 404 error. And if I change the ID but leave the movie name correct, like this: example.com/movies/123/Interstellar then WordPress still loads the correct page.

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

Based on this behavior, it's safe to say WordPress loads the page based on the page slug from the URL, rather than the movie ID, and that is what I need to fix.

这是我目前的代码:

电影插件.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]',

If I remove the movie_name from the URL in add_rewrite_rule, WordPress just loads the archive page of that post type. 'index.php?post_type=movies&movie_id=$matches[1]',

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

If I use the page name in the URL rewrite, it always loads the page based on the name, rather than the movie_id. 'index.php?post_type=movies&movie_id=$matches[1]&name=$matches[2]',

推荐答案

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

I didn't test code below, but movies.php could be like this:

<?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;

这篇关于WordPress - 通过带有自定义字段值的 url 请求发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆