Wordpress - Show only posts with specific slug - php

I'm sure this is going to be a silly question... but here it goes.
I currently have a page that only displays posts of a Custom Post Type (car).
I do this by running a query
$args = array(
'post_type' => 'car');
$query = new WP_Query( $args );
On to the loop...
For this Custom Post Type i have a Custom Taxonomy e.g. Subaru, Honda etc...
I'm just trying to work something else out, but if I wanted to show only posts that are Subaru's, how would I query that?
I guess I want to query the 'slug' (subaru), this code doesn't work, but you can see the route I was heading...
$args = array(
'name' => 'subaru',
'post_type' => 'car');
$query = new WP_Query( $args );
On to the loop...
I know name isn't right. What is the correct term to add to my $args array?
Many thanks

Depends on what your taxonomy is called. In my example its called 'brands':
$args = array(
'post_type' => 'car',
'brand' => 'subaru'
);
$query = new WP_Query( $args );
see http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

What you're trying to do is a taxonomy query. I could tell you how to do that but I think it's important I point out a much bigger mistake first. You shouldn't querying this on a page.
That's what archives are for.
Create a template file called archive-car.php and output there instead. That removes the need to run a custom WP Query.
Then create another file taxonomy-{your-custom-taxonomy-name}.php
Output the cars there as well and your problem is solved without adding any inefficient queries.

I think you need to use get_terms()
$terms = get_terms("Subaru");
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
echo "<ul>";
foreach ( $terms as $term ) {
echo "<li>" . $term->name . "</li>";
}
echo "</ul>";
}
See this http://codex.wordpress.org/Function_Reference/get_terms

Related

Wordpress: modifying archive with custom WP_Query

I want to create a custom category (archive) page for a specific set of categories, adding some querystring vars.
So, I have an url like http://example.com/services/restaurants?city=gotham
If I don't modify WP_Query I have a correct list of services/restaurants but I want to add a filter by city, so if I use WP_Query like this then I get all services (not only restaurants), so it's not working as expected.
This is my code:
<?php
$categories = explode("/", get_query_var('category_name'));
$the_query = new WP_Query(array(
'post_type' => 'services',
'category_and' => $categories,
'meta_query' => [['key' => 'city', 'value' => $_GET['city']]]
)
);
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<?php the_title(); ?>
<?php
endwhile;
This is showing all services, not only restaurants even $categories is an array with [0] => 'restaurants'.
Note: if I use SAVEQUERIES and print all $wpdb->queries I can see the correct queries are logged, but then, after this queries, other queries for all services are applied.
How can I use WP_Query to get the category marked by url and add my own meta keys?
Thank you.
Just use at top of your archive.php
if( isset( $_GET['city'] && ''!= $_GET['city'] ) {
$city = $_GET['city'];
global $query_string;
$posts = query_posts($query_string."&meta_key=city&meta_value=$city");
}
It will modify the original query and append it

Wordpress loop using two variables in array with category_name

I need some help working out where my formatting is wrong.
I have this code at the start of my page template;
<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$parent = get_term($term->parent, get_query_var('taxonomy') );?>
<?php echo do_shortcode("[ecs-list-events message='' cat='{$term->slug}']"); ?>
<?php echo do_shortcode("[ecs-list-events message='' cat='{$parent->slug}']"); ?>
Im now trying to use these variables "$term->slug" and "$parent->slug" in a loop which is currently, im pretty sure everything in the loop is as it should be as the first variable works but the second is ignored.
<?php
// WP_Query arguments
$args = array (
'category_name' => array ("$parent->slug", "$term->slug"),
);
// The Query
$welcome_text = new WP_Query( $args );
// The Loop
if ( $welcome_text->have_posts() ) {
while ( $welcome_text->have_posts() ) {
$welcome_text->the_post();
the_content();
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
When i try to format using both i getthe below i get an error like;
Warning: urlencode() expects parameter 1 to be string, array given in /var/sites/s/silverfx.co.uk/public_html/wp-includes/formatting.php
any ideas on where im going wrong?
Thanks
[EDIT]
I have just discovered that category_name only accepts strings so i guess the questions is now why does it ready the first variable and not the second?
it doesnt matter which way round they are ordered, the first always works and the second is ignored.
Current formatting is;
'category_name' => $parent->slug, $term->slug);
It only accepts string separated by , or +.
Your args should be:
$cats = [];
$cats[] = $parent->slug;
$cats[] = $term->slug;
'category_name' => implode(",", $cats));
Or
'category_name' => $parent->slug. '+'. $term->slug);
You can read more about it here.

PHP function, needed few input for post_type(wordpress theme function)

Function within theme function of wordpress
function child_post_list($title){
$args = array('posts_per_page'=>1, 'post_type' => 'updatelist');
$update_list_post = get_posts( $args ); //UPDATE LIST post
if(isset($update_list_post[0])){
$args = array('meta_key'=>'_wpcf_belongs_updatelist_id', 'meta_value' => $update_list_post[0]->ID,'post_type' => "'indon-stat';'myanmar-stat'");
$child_posts = get_posts( $args ); //the child listing of UPDATE LIST post
foreach($child_posts as $post){
if(get_post_meta($post->ID, 'wpcf-biodata-code', true) == $title){ //if the post title is exist within the custom field of the child listing of UPDATE LIST post.
return 1;
}
}
}
}
As you can see, the last part of the code
"'indon-stat';'myanmar-stat'");
This is definitely wrong syntax, so I would like to ask what is the correct one? And is it even possible to insert more than one post-type for this?
If you want to insert more than one post types , then try to insert it as comma seperated values or as a JSON string
'post_type' => "indon-stat,myanmar-stat"

what is wrong with this code - get taxonomy from theme option

I need to query posts from custom post taxonomy (set by user from WordPress theme option panel).
I am using the following code:
<?php
if ( function_exists( 'get_option_tree') ) {
$taxonomy = get_option_tree( 'taxonomy_option' );
}
$args = array(
'project_type' => $taxonomy,
'show_count' => 6,
);
query_posts($args);
?>
The code is working just if I enter on 'project_type' => 'my taxonomy name'. What is wrong with the code above? How I can get an option (option-tree) into args array? Later edit: the optiontree function render the taxonomy ID.
thanks
The function get_option_tree() might be giving you an array, you should check it out with
echo "<pre>".print_r($taxonomy,true)."</pre>";
Update:
After some discussion with #Ad Reactor, we found out that the $taxonomy output was a number instead of a slug. To get the slug, we can use
$term = get_term( 1234, 'project_type' );
$slug= $term->slug;
where 1234 is replaced with the real term_id
The only problem I can see is $taxonomy is not initialised and because of that if your condition does not met the $taxonomy will be undefined.
So define $taxonomy above your if condition.
$taxonomy = '';
if ( function_exists( 'get_option_tree') ) {
.....

Wordpress select custom post by metabox

So I have a custom post called event, but I need to query only those posts which have a custom metabox(is a checkbox) called `Show On The Homepage: and only if that checkbox is checked.
<?php
$args = array( 'post_type' => 'Event','posts_per_page' => 1000 );
$loop = new WP_Query( $args );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
My question is there a way to query this, or I will have to do a get_meta in the loop and check each one with an if statement
I'll go ahead and answer this anyway
Your answer is here: http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
So use meta_key and meta_value in your args

Categories