使用具有自定义类型的自定义 SQL 查询无法进行分页
我已经为这个问题苦苦挣扎了两天了。我读了几十篇文章,但无法找到解决方案。
注意:所有变量名称都是西班牙语,因为这是一个西班牙语网站。
我创建了一个名为“promocion”的自定义类型,但是当我尝试转到第 2 页时列出档案时,出现 404 错误。
我想设置的结构如下:
- domain.com/promocion/new-promocion -> 这效果很好
- domain.com/promociones ->全部清单 promociones,这也很好用
- domain.com/promociones/page/2 -> 错误 404 - 未找到
- 我的存档文件的名称 模板:archive-promocion.php
- 我的单页视图的名称 模板:single-promocion.php
WordPress 版本:3.1
插件:
- wp-page-navi
- 帖子 2 帖子插件 (http://wordpress.org/extend/plugins/posts-to-posts/), 用于创建之间的关系 帖子和促销。
这是在functions.php中创建的自定义类型
register_post_type('promocion', array(
'label' => 'Promociones',
'description' => 'Promociones',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array('slug' => 'promocion'),
'query_var' => true,
'has_archive' => 'promociones',
'menu_position' => 4,
'supports' => array('title','editor',),'labels' => array (
'name' => 'promociones',
'singular_name' => 'promocion',
'menu_name' => 'Promociones',
'add_new' => 'Añadir nueva',
'add_new_item' => 'Añadir nueva',
'edit' => 'Editar',
'edit_item' => 'Editar Promoción',
'new_item' => 'Nueva Promoción',
'view' => 'Ver Promoción',
'view_item' => 'Ver Promoción',
'search_items' => 'Buscar Promociones',
'not_found' => 'No se encontraron promociones',
'not_found_in_trash' => 'No se encontraron promociones en la papelera',
'parent' => 'Parent Promoción',),) );
function my_connection_types() {
if ( !function_exists( 'p2p_register_connection_type' ) )
return;
p2p_register_connection_type( array(
'from' => 'promocion',
'to' => 'post',
'reciprocal' => true
) );
}
add_action( 'init', 'my_connection_types', 100 );
这是我的存档页面(archive-promocion.php)的开头,我在其中执行自定义SQL查询并设置分页:
if ( $cat != '' ) {
$cat_filter = 'wp_term_taxonomy.term_id = "' . $cat . '" AND';
} else {
$cat_filter = '';
}
$querystr = '
SELECT DISTINCT
promociones.ID,
promociones.post_title
FROM
wp_terms
Inner Join wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
Inner Join wp_term_relationships AS wpr ON wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
Inner Join wp_posts AS comercios ON comercios.ID = wpr.object_id
Inner Join wp_p2p ON wp_p2p.p2p_to = comercios.ID
Inner Join wp_posts AS promociones ON promociones.ID = wp_p2p.p2p_from
WHERE
wp_term_taxonomy.taxonomy = "category" AND
comercios.post_type = "post" AND
' . $cat_filter . '
promociones.post_type = "promocion"
ORDER BY
promociones.menu_order ASC
';
$totalposts = $wpdb->get_results($querystr, OBJECT);
$ppp = 2;
$wp_query->found_posts = count($totalposts);
$wp_query->max_num_pages = ceil($wp_query->found_posts / $ppp);
$on_page = intval(get_query_var('paged'));
if($on_page == 0){ $on_page = 1; }
$offset = ($on_page-1) * $ppp;
$wp_query->request = $querystr . " LIMIT $ppp OFFSET $offset";
$pageposts = $wpdb->get_results($wp_query->request, OBJECT);
.htaccess文件
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /sitiodeloschicos/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /sitiodeloschicos/index.php [L]
</IfModule>
请帮助我,我在这里要疯了我这个项目已经迟到了。谢谢!
I've been struggling with this problem for 2 days now. I've read dozens of posts but can't get to the solution.
Note: all the var names are in spanish since this is a spanish website.
I've created a custom type named "promocion", but when listing the archives when I try to go to page 2 I get a 404 error.
The structure I'd like to set up is the following:
- domain.com/promocion/new-promocion ->
this works well - domain.com/promociones -> list of all
the promociones, this works well too - domain.com/promociones/page/2 ->
Error 404 - Not Found - Name of the archive file in my
template: archive-promocion.php - Name of the single page view in my
template: single-promocion.php
WordPress version: 3.1
Plugins:
- wp-page-navi
- Posts 2 Posts plugin
(http://wordpress.org/extend/plugins/posts-to-posts/),
used to create a relation between
posts and promociones.
Here's the custom type created in functions.php
register_post_type('promocion', array(
'label' => 'Promociones',
'description' => 'Promociones',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array('slug' => 'promocion'),
'query_var' => true,
'has_archive' => 'promociones',
'menu_position' => 4,
'supports' => array('title','editor',),'labels' => array (
'name' => 'promociones',
'singular_name' => 'promocion',
'menu_name' => 'Promociones',
'add_new' => 'Añadir nueva',
'add_new_item' => 'Añadir nueva',
'edit' => 'Editar',
'edit_item' => 'Editar Promoción',
'new_item' => 'Nueva Promoción',
'view' => 'Ver Promoción',
'view_item' => 'Ver Promoción',
'search_items' => 'Buscar Promociones',
'not_found' => 'No se encontraron promociones',
'not_found_in_trash' => 'No se encontraron promociones en la papelera',
'parent' => 'Parent Promoción',),) );
function my_connection_types() {
if ( !function_exists( 'p2p_register_connection_type' ) )
return;
p2p_register_connection_type( array(
'from' => 'promocion',
'to' => 'post',
'reciprocal' => true
) );
}
add_action( 'init', 'my_connection_types', 100 );
And here's the beginning of my archive page (archive-promocion.php) where I do a custom SQL query and set the pagination:
if ( $cat != '' ) {
$cat_filter = 'wp_term_taxonomy.term_id = "' . $cat . '" AND';
} else {
$cat_filter = '';
}
$querystr = '
SELECT DISTINCT
promociones.ID,
promociones.post_title
FROM
wp_terms
Inner Join wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
Inner Join wp_term_relationships AS wpr ON wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
Inner Join wp_posts AS comercios ON comercios.ID = wpr.object_id
Inner Join wp_p2p ON wp_p2p.p2p_to = comercios.ID
Inner Join wp_posts AS promociones ON promociones.ID = wp_p2p.p2p_from
WHERE
wp_term_taxonomy.taxonomy = "category" AND
comercios.post_type = "post" AND
' . $cat_filter . '
promociones.post_type = "promocion"
ORDER BY
promociones.menu_order ASC
';
$totalposts = $wpdb->get_results($querystr, OBJECT);
$ppp = 2;
$wp_query->found_posts = count($totalposts);
$wp_query->max_num_pages = ceil($wp_query->found_posts / $ppp);
$on_page = intval(get_query_var('paged'));
if($on_page == 0){ $on_page = 1; }
$offset = ($on_page-1) * $ppp;
$wp_query->request = $querystr . " LIMIT $ppp OFFSET $offset";
$pageposts = $wpdb->get_results($wp_query->request, OBJECT);
.htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /sitiodeloschicos/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /sitiodeloschicos/index.php [L]
</IfModule>
Please help me, I'm going insane here and I'm already late with this project. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,经过 1 周,是的,1 周的拉扯我的头发,阅读互联网上的每个论坛并询问每个人,我找到了解决方案,这非常愚蠢。
上面发布的所有代码都运行完美,没有任何问题。
代码中的问题在于我在管理面板中设置为每页显示 10 个帖子。在我的代码中,出于测试目的,我将其更改为每页仅显示 1,但不知何故,wordpress 仍然使用管理面板中配置的 10 值。
所以我所做的就是在管理面板中将值更改为 1,然后在代码中增加我的值。这是一个令人讨厌的解决方案,但它确实有效。
有没有办法通过代码设置每页帖子的全局值?
Well, after 1 week, yes, 1 week of pulling my hair, reading every forum on the Internet and asking everybody, I found the solution, which is pretty dumb.
All the code posted above is working perfect, there's nothing wrong with it.
What was messing with the code is that I had setup in the admin panel to display 10 posts per page. In my code I changed it to display only 1 per page for testing purposes, but somehow wordpress was still using the 10 value configured in the admin panel.
So what I did is I changed the value to 1 in the admin panel, and then increased mine in the code. This is a nasty solution but it works.
Is there any way to setup the global value of posts per page by code?
我的猜测是,这是因为您正在使用自定义 SQL 来获取自定义帖子类型。通过使用 WP_Query 类,您可以以更标准的 WP 方式执行查询。我将使用它来获取我的自定义帖子:
如果您像这样执行查询,您将能够使用内置的 WP 函数来获取下一个 (http://codex.wordpress.org/Function_Reference/next_post_link) 和上一个帖子(http://codex.wordpress.org/Template_Tags/previous_post_link)。此外,您可以利用这个很棒的分页插件(http://wordpress.org/extend/plugins/wp-paginate/)。
请务必检查 WP_Query 类可以执行的所有操作: http://codex.wordpress.org/ Function_Reference/WP_Query。
My guess is that this is because you are using custom SQL to get your custom post type. By using the WP_Query class, you can perform the query in a more standard WP way. I would use this to get my custom posts:
If you perform your query as such, you will be able to use the built in WP functions for getting the next (http://codex.wordpress.org/Function_Reference/next_post_link) and previous posts (http://codex.wordpress.org/Template_Tags/previous_post_link). Additionally, you could take advantage of this awesome pagination plugin (http://wordpress.org/extend/plugins/wp-paginate/).
Make sure to check out all that you can do with the WP_Query class: http://codex.wordpress.org/Function_Reference/WP_Query.
如果您收到带有漂亮永久链接的 404 错误,那是因为您尚未创建自定义帖子类型存档模板文件。自定义帖子类型不会为其提取默认模板文件。因此,您必须创建 archive-post_type_name.php、single-post_type_name.php 等以避免 404。
如果您希望类别与您的自定义帖子类型相关联,那么您需要 register_taxonomy 并添加仅适用于您的自定义帖子类型的类别。 ( http://codex.wordpress.org/Function_Reference/register_taxonomy 和 http://justintadlock.com/archives/2010/06/10/a-refresher -on-custom-taxonomies)
然后您可以使用常规的 WP_Query。
无论如何,当您 query_posts 时,您将删除所有分页功能。你必须添加它。即:
If you're getting a 404 error with pretty permalinks, it's because you haven't created a custom post type archive template file. Custom post types don't pull default template files for them. So you have to create archive-post_type_name.php, single-post_type_name.php, etc. to avoid the 404's.
If you want categories associated with your custom post type, then you need to register_taxonomy and add categories that are meant solely for your custom post type. ( http://codex.wordpress.org/Function_Reference/register_taxonomy and http://justintadlock.com/archives/2010/06/10/a-refresher-on-custom-taxonomies)
Then you can use the regular WP_Query.
In any case, when you query_posts, you're removing all capabilities for pagination. You have to add it in. i.e.: