how build a wordpress query_posts based on url - wordpress

I want to use the query_posts function from wordpress for displaying the right posts. I want to use the url www.site.com/?s=taxonomy1=test1&taxonomy2=test2 for building the query. something like down here:
$taxonomy1 = $_GET['taxonomy1'];
$taxonomy2 = $_GET['taxonomy2'];
query_posts(array(
'posts_per_page' => 10,
'taxonomy1' => $taxonomy1,
'taxonomy2' => $taxonomy2,
) );>
How i do this?

the wordpress codex is your best friend when trying to build custom queries. something like this should work as a query
$taxonomy1 = $_GET['taxonomy1'];
$taxonomy2 = $_GET['taxonomy2'];
$the_query = new WP_Query(array(
'posts_per_page' => 10,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => $taxonomy1,
'field' => 'slug',
),
array(
'taxonomy' => $taxonomy2,
'field' => 'slug',
),
),
));
and to display the results
while ( $the_query->have_posts() ) : $the_query->the_post();
//the_title(), the_content(), etc
endwhile;
note that the query is using the new (as of 3.1) method for querying taxonomies. As you can see it gives you a lot more flexibility and i would suggest reading through the above link

Related

How to display post with two custom taxonomy in wordpress

I have a custom post type - Poems.
And I create a two taxonomy - Cities and Authors.
When I clicked on "Paris" menu item - I need to displayed all Authors and his list of poems in that city.
How to do it ?
Thanks.
You'll likely want to take a look at the Codex entry for WP Query. That gives you all the information you could want on creating custom WordPress queries. Including taxonomy arguments.
You'd want to pass to tax query arguments using the AND relation. Something along the lines of this example:
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => 'london',
),
array(
'taxonomy' => 'author',
'field' => 'slug',
'terms' => 'dickens',
),
),
);
$query = new WP_Query( $args );
Edit: 10/30/17 per comment below
If you were saving your author's as post meta you could order by that. But if your author is also a taxonomy it gets a little trickier since there isn't a good way to order by a taxonomy. See this related question.
Instead we could get all of our author taxonomy terms using get_terms.
$authors = get_terms( 'author', array(
'hide_empty' => false,
) );
That gives us all author terms that have at least one post assigned to them.
Now that we have the terms we could run through each author and run a query to get the poems for that author in that city. Like so:
foreach ( $authors as $author ) {
$args = array(
'post_type' => 'poem',
'tax_query' => array(
relation => 'AND',
array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => 'london',
),
array(
'taxonomy' => 'author',
'field' => 'term_id',
'terms' => $author->term_id,
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
// Put your post markup here
endwhile;
endif; wp_reset_query();
}
If you are not good with coding you can use free Content Views plugin, and combine that yyou have to combine. I started like this.

Wordpress: Use page variable with custom field inside WP_Query

I have the following variable that I'm trying print in the tax query array section "term =>"... The field is a custom field attached to the page.
I can print $value_variable before the wp_query, but when I put it inside the array it does not print.
This is in a Wordpress environment.
$value_variable = the_field('categoria_de_slider');
$argumentsalojamientos1fa = array(
'post_type' => 'slider_tours',
'posts_per_page' => '-1',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'asignar_slider',
'field' => 'id',
'term' => $value_variable
)
)
);
$queryalojamiento1fa = new WP_Query($argumentsalojamientos1fa);
while($queryalojamiento1fa->have_posts()) : $queryalojamiento1fa->the_post();?>
<?endwhile; ?>
// something happens
<?php wp_reset_postdata(); ?>
<?php rewind_posts(); ?>
I did further studies of my code and found 2 problems. The answer is not the answer to the question, because it was improper code what caused the problem.
The correct way is using get_field.
$valuevariable = get_field('categoria_de_slider');
My tax query was wrong too, my $valuevariable value is the category term ID so I had to use "terms_id" and "terms".
'tax_query' => array(
array(
'taxonomy' => 'asignar_slider',
'field' => 'terms_id',
'terms' => $valuevariable
)
);

Can't get tax_query to work

I have a custom WP Query where I list custom post types. I am trying to set up filters to filter listings by custom taxonomy.
My query setup is as follows:
// WP_Query arguments
$args = array (
'post_type' => 'opps',
'post_status' => 'publish',
'pagination' => true,
'posts_per_page' => $postsperpage,
'posts_per_archive_page' => $postsperpage,
'ignore_sticky_posts' => true,
'order' => 'DESC',
'orderby' => 'date',
'cache_results' => true,
'update_post_meta_cache' => true,
'update_post_term_cache' => true,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'sector',
'field' => 'slug',
'terms' => 'business-services'
),
)
);
So in the above example I should get only posts with taxonomy 'sector' with slug 'business-services', but ALL posts from custom post types 'opps' get listed instead.
I have looked everywhere on stackoverflow and beyond but I just can't figure out what I've been doing wrong.
Your code looks fine.
I have had this issue before because wordpress does not sync taxonomy terms until late in the init phase. Have you tried running this code in a template, after get_header() or wp_head() has been called? If not try that, it should work then. I know if I've echoed it out in functions.php for testing it will not work because wordpress has not synced tax terms yet.
You should really have a look at the codex (WP_Query) when creating custom queries. As your code stands, it will fail. You have the following issues
pagination is not a valid parameter for WP_Query.
You cannot use posts_per_page and posts_per_archive_page together
You don't need to set parameter values that are set by default. Unnecessary parameters used in your code which is by default the default values are order, orderby, post_status, cache_results, update_post_meta_cache and update_post_term_cache
There is no sticky post functionality for custom post types. just remove the ignore_sticky_post parameter
Your code should look something like this ( I hope your variables is defined )
// WP_Query arguments
$args = array (
'post_type' => 'opps',
'posts_per_page' => $postsperpage,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'sector',
'field' => 'slug',
'terms' => 'business-services'
),
)
);

Return all posts for a custom taxonomy id in wordpress

I have this code that should return all the posts that related to the taxonomy id, but it returns the last 5 posts.
<?php
$leaderships = new WP_Query(array(
'post_type' => 'leadership',
'posts_per_page' => 11,
'tax_query' => array(
array(
'taxonomy' => 'leadership-committee',
'field' => 'id',
'terms' => 13,
),
),
));
?>
posts_per_page is not working here, Any help to get all the posts.
Thanks
Here is the answer of the above question...
$args = array('post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => 619,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_title().''."<br/>";
endwhile;
}
Try testing the simplest way possible:
<?php
$leaderships = new WP_Query(array(
'post_type' => 'leadership',
'posts_per_page' => -1
));
?>
If it returns all posts from "leadership" custom post type, then narrow it with 'tax_query' and check if You have more than 5 entries of custom post type "leadership" that are in custom taxonomy named "leadership-committee" in its created child (category/tag like) with id of 13.
Beside that, all looks ok in query.
Thanks every one for help, I discovered that that the problem is from my theme admin area that was limiting the posts to 5 and fixed now.

Only show WordPress 'standard' post format on template?

I recently added post formats to my WordPress theme - on the blog page its fine as they are all styled accordingly. However on my home page template I only want to show 'standard' posts formats (no links, galleries, audio, video etc.).
In my theme options I can decide how many posts to display on the front page which is what 'dft_recent_number' is for.
Does anybody know how I can change the code below to exclude all but 'standard' post formats?
<?php
$query = new WP_Query();
$query->query('posts_per_page='.get_option('dft_recent_number'));
//Get the total amount of posts
$post_count = $query->post_count;
while ($query->have_posts()) : $query->the_post();
?>
Any help is much appreciated!
WP_Query does not seem to have a straightforward parameter for post_format.
From my quick research it appears that post formats are related through taxonomies. So, theoretically, using the taxonomy parameters should work.
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => 'post-format-standard',
)
)
);
$query = new WP_Query( $args );
Note: you'll need to update the taxonomy names and slugs for your blog. This should be the names you set in your functions.php file.
// there's no post-format-standard so you should write it like this to exclude all other postpformats
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array('post-format-quote','post-format-audio','post-format-gallery','post-format-image','post-format-link','post-format-video'),
'operator' => 'NOT IN'
)
I know that's old, but I was facing the same issue and even though I've found a solution I wondered what other have done to "fix" that.
I think a more scalable solution could be something like that:
$post_formats = get_theme_support( 'post-formats' );
$tax_query = false;
if ( $post_formats ) {
$tax_query = array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => $post_formats[0],
'operator' => 'NOT IN'
)
);
}
// WP_Query arguments
$args = array(
'post_type' => 'post',
'order' => 'DESC',
'orderby' => 'date',
'tax_query' => $tax_query
);
This would exclude the post formats that have been enabled and also will work in case WP will add more post formats (or add the ability to add more).

Resources