Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

merge taxonmies single query filter by meta vaule WordPress

  • SOLVED

I have the below query that works but i want to also include the taxonomy 'blogger_category'
I tried adding an array to $tax = but that duplicated the post from both job and blogger

Also in each post type there is a custom field 'ispaid' i want to sort this query post by this 'ispaid' field if it is true or not (0 or 1 ) ...

so basically i'm pulling two post types 'job' and 'blogger' and getting the terms from each whilst sorting by ispaid...

thanks


<?php
//for a given post type, return all
$post_type = array ('job', 'blogger');
$tax = 'job_category';
$tax_terms = get_terms($tax);
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args=array(
'post_type' => $post_type,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'ASC',
'caller_get_posts'=> 1


); // END $args

$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {

while ($my_query->have_posts()) : $my_query->the_post(); ?>

<li>

<span class="col3 area"><?php if (get_post_type( get_the_ID() ) == 'job') { ?> Need Bloggers <?php } else echo 'Need a Job'; ?></span>
<span class="col3 areaDesc"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></span>
<span class="col3 areaCat"><?php echo $tax_term->name; ?> </span>
</li>

Answers (5)

2013-11-09

Remy answers:


$job_categories = array();
$job_terms = get_terms( 'job_category' );
foreach( $job_terms as $job_term) {
$job_categories[] = $job_term->term_id;
}

$blog_categories = array();
$blog_terms = get_terms( 'blogger_category' );
foreach( $blog_terms as $blog_term) {
$blog_categories[] = $blog_term->term_id;
}

$job_category_query = array( taxonomy' => 'job_category', 'field' => 'id', 'terms' => $job_categories );
$blog_category_query = array( taxonomy' => 'job_category', 'field' => 'id', 'terms' => $blog_categories );

$my_query = new WP_Query( array(
'post_type' => array('job', 'blogger'),
'ignore_sticky_posts' => 1,
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'meta_value_num' ,
'meta_key' => 'ispaid',
'order' => 'ASC',
'tax_query' => array(
'relation' => 'OR',
$job_category_query,
$blog_category_query,
)
);


James Dean comments:

thanks, but i get PHP Parse error: syntax error, unexpected ';' on line 87 ( the last line of your code )

any ideas?

2013-11-06

Sébastien | French WordpressDesigner answers:

$args = array(
'post_type' => array ('job', 'blogger'),
'meta_key' => 'ispaid',
'orderby' => 'meta_value_num',
'paged' => $paged,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'job_category',
'field' => 'slug'
),
array(
'taxonomy' => 'blogger_category',
'field' => 'slug'
),
),
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'ASC',
'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();


James Dean comments:

doesnt seem to work


James Dean comments:


<ul class="tblHeader">
<li>
<span class="col3 area">Ad Type</span>
<span class="col3 areaDesc">Title</span>
<span class="col3 areaCat last">Category</span>
</li>
</ul>

<ul class="tblContent">

<?php
$args = array(

'post_type' => array ('job', 'blogger'),

'meta_key' => 'ispaid',

'orderby' => 'meta_value_num',
'paged' => $paged,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'job_category',
'field' => 'slug'
),
array(
'taxonomy' => 'blogger_category',
'field' => 'slug'
),
),
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'ASC',
'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();
?>

<li>

<span class="col3 area"><?php if (get_post_type( get_the_ID() ) == 'job') { ?> Need Bloggers <?php } else echo 'Need a Job'; ?></span>
<span class="col3 areaDesc"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></span>
<span class="col3 areaCat"><?php echo $tax_term->name; ?> </span>
</li>




</ul>





Sébastien | French WordpressDesigner comments:

$args = array(
'post_type' => array ('job', 'blogger'),
'meta_key' => 'ispaid',
'orderby' => 'meta_value_num',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'job_category',
'field' => 'slug'
),
array(
'taxonomy' => 'blogger_category',
'field' => 'slug'
),
),
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'ASC',
'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();


use this code
and change 'orderby' => 'meta_value_num',
by 'orderby' => 'meta_value'

if that doesn't work, remove
'meta_key' => 'ispaid',
'orderby' => 'meta_value_num',

and try again


James Dean comments:

nope nothing


James Dean comments:

i was hoping for a complete answer...

i've no longer got my for each statement in there nor can i echo my tax terms as i orginally had

now the code you provide me returns nothing..


James Dean comments:

not working

2013-11-06

Arnav Joy answers:

try this

<?php


//for a given post type, return all

$post_type = array ('job', 'blogger');

$tax = 'job_category';

$tax_terms = get_terms($tax);

if ($tax_terms) {

foreach ($tax_terms as $tax_term) {

$args=array(

'post_type' => $post_type,

"$tax" => $tax_term->slug,

'post_status' => 'publish',

'posts_per_page' => -1,

'order' => 'ASC',

'caller_get_posts'=> 1 ,

'orderby' => 'meta_value_num' ,

'meta_key' => 'ispaid',

'order' => 'ASC'





); // END $args



$my_query = null;

$my_query = new WP_Query($args);


James Dean comments:

I think you forgot to add the tax 'taxonomy' => 'blogger_category',


James Dean comments:

I think you forgot to add the 'taxonomy' => 'blogger_category',


James Dean comments:

not working can you provide correct answer please

2013-11-08

chevy answers:



<?php

//for a given post type, return all

$post_type = array ('job', 'blogger');

$tax = 'job_category';

$tax_terms = get_terms($tax);

if ($tax_terms) {

foreach ($tax_terms as $tax_term) {

$args=array(

'post_type' => $post_type,

"$tax" => $tax_term->slug,

'post_status' => 'publish',

'posts_per_page' => -1,

'order' => 'ASC',

'caller_get_posts'=> 1





); // END $args


global $wp_query;
$args = array_merge( $wp_query->query_vars, array( 'post_type' => 'product' ) );
query_posts( $args );

query_posts( 'cat=3&year=2004' );

if ( is_home() ) {
query_posts( $query_string . '&cat=13&monthnum=' . date( 'n', current_time( 'timestamp' ) ) );
}

query_posts( array( 'category__and' => array(1,3), 'posts_per_page' => 2, 'orderby' => 'title', 'order' => 'DESC' ) );


query_posts( 'cat=1&tag=apples' );


query_posts( 'cat=1&tag=apples+oranges' );


<li>

<span class="col3 area"><?php if (get_post_type( get_the_ID() ) == 'job') { ?> Need Bloggers <?php } else echo 'Need a Job'; ?></span>

<span class="col3 areaDesc"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></span>

<span class="col3 areaCat"><?php echo $tax_term->name; ?> </span>

</li>

2013-11-08

letsassist answers:

$args = array(
'post_type' => array ('job', 'blogger'),
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'job_category',
'field' => 'slug'

)
)
);