admin管理员组文章数量:1122846
I want to create custom links like
mydomain/custom_page/cat=ABC&tag=XYZ
So that when a user clicks on the link s/he can see all posts in the category 'ABC' having tag 'XYZ'
For this I've created a custom template with the following code
<?php
/*
Template Name: MyCustomTemplate
*/
?>
<?php get_header(); ?>
global $wp_query;
get_query_var( 'cat' );
get_query_var( 'tag' );
<?php get_footer(); ?>
I don't know how to query for the posts in the category 'ABC' with the tag 'XYZ'
I checked
but the examples shown there use 'static' values. I need to query using dynamic values: which are passed via the URL.
Also, I'm using a plugin 'Advanced Custom Fields' and have added a field 'priority' with the defult value 'Z'. I intend to assign one alphabet to each post in the priority field, so that results on the page are served sorted according to "priority" : Posts with the priority 'A' on the top, followed by posts with priority 'B' and so on..
In Short:
I want to get category and tag parameters from links like:
mydomain/custom_page/cat=some_category&tag=some_tag
Then Fetch posts in the category 'some_category' AND having tag 'some_tag' AND sorted according to custom field : 'priority'
I want to create custom links like
mydomain.com/custom_page/cat=ABC&tag=XYZ
So that when a user clicks on the link s/he can see all posts in the category 'ABC' having tag 'XYZ'
For this I've created a custom template with the following code
<?php
/*
Template Name: MyCustomTemplate
*/
?>
<?php get_header(); ?>
global $wp_query;
get_query_var( 'cat' );
get_query_var( 'tag' );
<?php get_footer(); ?>
I don't know how to query for the posts in the category 'ABC' with the tag 'XYZ'
I checked http://codex.wordpress.org/Function_Reference/query_posts#Passing_variables_to_query_posts
but the examples shown there use 'static' values. I need to query using dynamic values: which are passed via the URL.
Also, I'm using a plugin 'Advanced Custom Fields' and have added a field 'priority' with the defult value 'Z'. I intend to assign one alphabet to each post in the priority field, so that results on the page are served sorted according to "priority" : Posts with the priority 'A' on the top, followed by posts with priority 'B' and so on..
In Short:
I want to get category and tag parameters from links like:
mydomain.com/custom_page/cat=some_category&tag=some_tag
Then Fetch posts in the category 'some_category' AND having tag 'some_tag' AND sorted according to custom field : 'priority'
Share Improve this question edited Sep 12, 2013 at 6:03 Gaurav Srivastava asked Sep 12, 2013 at 4:15 Gaurav SrivastavaGaurav Srivastava 11 silver badge2 bronze badges 3- 1 Can you just make your question more clear with entering only required information in the question? – Vinod Dalvi Commented Sep 12, 2013 at 4:17
- Sorry for the ambiguous question. I have added a summary in the end of the post. If that doesn't help, please mention what clarifications you need. – Gaurav Srivastava Commented Sep 12, 2013 at 6:05
- FYI, WordPress already possesses the ability to filter post archives on multiple taxonomies without having to run custom queries. see this post. – Milo Commented Sep 12, 2013 at 6:32
3 Answers
Reset to default 1Clarification of your question:
You want to show Posts of a certain category named "ABC" AND also you want to filter the posts with the tag named "XYZ".
Answer:
If this is the scenario, have you tried: WP_Query()
?
<?php
// The Query
$args = array('post_type' => 'post',
'category_name' => 'ABC',
'tag' => 'XYZ',
'meta_key' => 'priority',
'orderby' => 'meta_value_num'
);
$the_query = new WP_Query($args);
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>
Something like this may solve your problem.
NOTE: I din't test this one, try yourself with it. If you face any problem, please comment here, or if you find any solution, don't forget to share that with me here.
Thanks.
You can get variables from $_REQUEST, but you must first reset permalinks to default format and make sure this is correct variable names. In your case it's $_REQUEST['cat'] and $_REQUEST['tag'], so the dynamic query will be
$args = array(
'post_type' => 'post',
'category_name' => sanitize_title_for_query( $_REQUEST['cat'] ),
'tag' => sanitize_title_for_query( $_REQUEST['tag'] ),
'meta_key' => 'priority',
'orderby' => 'meta_value_num'
);
you are not preparing your query correctly. First of all the data should be fetched associatively and you are displaying it differently. for code practice its bad.|
regarding your actual error. your query seems good but once again do this associatively. so your query should look like $post_tag => 'ABC'. then prepare, sanitize your query and push it into your database.
本文标签: custom query to get posts
版权声明:本文标题:custom query to get posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736311777a1934860.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论