WP Query - Unable to query by category slug - php

I have four categories in my Custom Post Type of "projects" that are 'live', 'heal', 'work', and 'play'. I have a few 'projects' posts with no category that I would like to omit from the archive page. In my archive, my query is as follows:
<?php $args = array( 'post_type' => 'projects','post_status' => 'publish', 'posts_per_page' => $projects_number, 'paged'=> $paged, 'category_name' => 'work,play,live,heal'); ?>
<?php $wp_query = null; ?>
<?php $wp_query = new WP_Query( $args ); ?>
<?php if ( $wp_query->have_posts() ) : ?>
But my archive is not returning any projects. I also tried the 'cat' parameter with id's but no avail there either. What am I missing?

For anyone who comes across this...the 'category' argument needed to be in its own array

Related

WP Sorting custom posts alphabetically by Title

Newbie here...
I have a custom post type of 'equipe' (team in portuguese). I am trying to sort these alphabetically by post title then display the_title so we have a alphabetical list of names.
I've done a search on here and tried a few fixes but Im struggling to get anything other that the standard order.
Any help would be much appreciated!
<?php
$args = array('orderby'=> 'title', 'order' => 'ASC', 'post_type' => 'equipe', 'posts_per_page' => -1, 'post_status' => 'publish' );
$q = new WP_Query($args);
while ( $q->have_posts() ) : $q->the_post();
?>
<h3><?php the_title(); ?></h3>
<?php
endwhile;
wp_reset_query();
?>
<?php
$args = array( 'post_type' => 'equipe', 'posts_per_page'=>5, 'orderby'=>'post_title','order'=>'ASC');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
RESOLVED:
Ok the reason it was enforcing menu_order was because of a setting (F*ing checkbox) within the plugin Post Types Order.
I needed to un-check AUTO SORT
and check Use query ASC / DESC parameter
This then allowed me to adjust the array as follow (and discussed above):
$args = array('orderby' => 'title', 'order'=>'ASC', 'post_type' => 'equipe')
However I did need to add 'order'=>'ASC' into the other pages that sorted by the original query of menu_order.

Getting all posts matching custom post type, taxonomy and category

I currently have the following set up:
Custom post type called "workshops"
Within the workshop post type I have a taxonomy called "workshop-status"
Workshop status subsequently has categories for example "past-event"
I have been using the code below to fetch posts within a particular post type:
<?php $loop = new WP_Query( array( 'post_type' => 'workshops', 'posts_per_page' => -1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
content here...
<?php endwhile; wp_reset_query(); ?>
My question is: how can I change this to fetch a post from the past-event category within my custom post type, and custom taxonomy?
My aim is to have multiple page templates and target each category individually.
I have tried changing the arrange to target the category alone, but this did not work. I cannot find an online resource on how to target all aspects.
You simply need to add the category attribute like so:
$query = new WP_Query( array( 'category_name' => 'past-event' ) );
So in your example case, this would become:
$loop = new WP_Query( array( 'post_type' => 'workshops', 'posts_per_page' => -1, 'category_name' => 'past-event' ) );
You can do a whole load of stuff as detailed in the code
If I understand you correctly, you're looking for something like this:
$args = array(
'post_type' => 'workshops',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'workshop-status',
'field' => 'slug',
'terms' => array( 'past-event'),
'operator' => 'IN'
),
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
//do your stuff
endwhile;
endif;
wp_reset_postdata();

WordPress wp_pagenavi for CPT using custom wp_query() on another CPT single page

I know there were a lot of questions about how to use wp_pagenavi with custom wp_query() for CPTs, and I can handle it easily. But I've encountered a bit more complicated situation.
I have a CPT named 'project' and CPT named 'avtoritet_audiotape'. What I need to do - is to show all CPT's 'avtoritet_audiotape' using pagination, but on the single 'project' CPT - it's a kind of relation (for example while I am on page http://simpex/project/radioprogramma-avtoritet, where 'project' is my CPT name).
My code, which is working perfectly on any archive pages, or even single pages is:
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
Information related to my single CPT instance 'project'
<?php endwhile; ?>
<?php $loop = new WP_Query(
array(
'post_type' => 'avtoritet_audiotape',
'posts_per_page' => 6,
'paged' => get_query_var('paged'),
'meta_key' => 'audiotape_date',
'orderby' => 'meta_value_num',
) );
?>
<?php if ( $loop->have_posts() ): ?>
#Some stuff
<?php wp_pagenavi( array( 'query' => $loop ) ); ?>
<?php endif; ?>
So, pagination shows pagination links correctly, but when trying to access http://simpex/project/radioprogramma-avtoritet/page/2 - it redirects to http://simpex/project/radioprogramma-avtoritet
Is it possible in anyway? Thank you
plz try this way.
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$postArray = array('post_type' => 'room', 'posts_per_page' => 5, 'order' => 'DESC', 'paged' => $paged);
$roomPost = new WP_Query($postArray);
global $post;
if ($roomPost->have_posts()) {
while ($roomPost->have_posts()) : $roomPost->the_post(); ?>
<div class="post_title"> <?php the_title(); ?></div>
<?php
endwhile;
wp_pagenavi(array('query' => $roomPost));
}

Having trouble with wordpress custom taxonomy archive

I'm having a little trouble with a custom taxonomy template. I inherited a site that was developed by someone else and they use "Types" plugin to add some custom taxonomies.
Goal:
to have an archive template that shows only posts with a certain taxonomy term in it at example-domain.com/people/harrison-ford
Problem:
This code is bringing in posts that do not have the taxonomy selected.
Here's my full code:
<?php
$year = get_post_meta($post->ID, 'year', true);
$post_type = 'post';
$tax = 'people';
$tax_terms = get_terms( $tax );
if ($tax_terms) {
$args = array(
'post_type' => $post_type,
'people' => 'harrison-ford',
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1,
'orderby' => 'date',
'order' => DESC
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) : ?>
<h2 class="wwNews"><?php echo $tax_term->name; ?> News</h2>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<-- display stuff -->
<?php endwhile; // end of loop ?>
<?php endif; // if have_posts()
wp_reset_query();
}
?>
What are you expecting here? "$tax" is going to to be 'people' =>, which is going to overwrite 'harrison-ford' to the value of $tax_term->slug.
'people' => 'harrison-ford',
"$tax" => $tax_term->slug,
Furthermore, I don't know of any custom argument called people, I'm pretty sure that you want tax_query:
'tax_query' => array(
'taxonomy' => 'people',
'terms' => array('harrison-ford', $tax_term->slug)
)
Which will give you the results of all people matching harrison-ford and the value of $tax_term->slug within the taxonomy of people

Custom post types displaying all category posts instead of showing posts only from specific category

I am having issues with custom post type categories display.
I have created custom post type for review site
I want to show different categories in different tabs
but when I put any category of review in menu it shows all reviews
instead of showing reviews from specific category
For example:
I have created 2 categories in reviews
a) Games
b) software
Whenever I choose Games category it shows posts from software category too.
I had same issue with blog posts categories but I resolved that issue using code
in my category.php file
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$cat_id = get_cat_ID( single_cat_title(null, false) );
query_posts(array(
'post_type' => 'post',
'paged' => $paged,
'cat'=>$cat_id,
));
I have created taxonomy.php file for custom post type
<?php $mypost = array( 'post_type' => 'cpreviews','paged' => $paged);
$loop = new WP_Query( $mypost ); ?>
Can anyone please help us to understand what we need to do to display posts
according to categories for custom post types?
UPDATED CODE IN TAXONOMY.PHP but still have some issue:
I have changed above code under taxonomy.php to
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
//$currentTerm = $_GET[ 'term' ];
$cat_id = get_cat_ID( single_cat_title(null, false) );
$mypost = array('post_type' => 'cptreviews',
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'product_reviews_product_category',
'terms' => (''),
'field' => 'slug',
'name' =>'Product Category',
)
)
);
$loop = new WP_Query( $mypost ); ?>
Now whenever I put category in 'terms' => ('kids') like this it shows all posts under that category only. but I want to take that 'terms value' dynamically.
Try this one:
<?php
$type = 'cpreviews';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Supposing you have Custom Post Type: cpReviews --- Custom Taxonomy: RevCategories --- Create new review post and chose category from RevCategories. Query cpReviews will definitely show all the posts, you need to do some thing like this -----
query_posts(array(
'post_type' =>'cpreviews', //Custom_Post_TYpe
'showposts' => $limit,
'RevCategories' => 'Games',)); //Custom Post Type TAxonomy (can use page name here get_query_var('pagename'); for dynamic content
while (have_posts()): the_post(); global $post; echo the_title(); endwhile;
I have resolved this issue by creating taxonomy-{taxonomy}.php file & removed tax query code..it automatically takes given category..thanks all for your help
This will resolve this issue.
$args = array(
'post_type'=> 'post',
'cat' => 'Games'
);
$the_query = new WP_Query( $args );
if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();

Categories