Wordpress: Use page variable with custom field inside WP_Query - wordpress

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
)
);

Related

Use ACF Taxonomy Field as Variable in Wordpress Custom Post Type Loop

I'm attempting to create a custom Wordpress query, using an Advanced Custom Fields field as a variable in the loop.
The use case is a page has a custom loop on it. For example, a page about a venue displays a loop of events (the custom post type) at the bottom of the page. I want for the person creating the page to choose what event tag (a tag taxonomy on the CPT) they want to attach to the page. Then the loop runs with that field attaching to the tag query used as a variable.
Here's my code so far:
<?php if ( get_field('event_tag') ) : ?>
<?php
$event_tag = get_field('event_tag');
$args = array(
'post_type' => 'events',
'posts_per_page' => 3,
'tag_id' => $event_tag,
'meta_key' => 'event_start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'ignore_sticky_posts' => true
);
?>
<?php echo $event_tag; ?><!-- This is only here to check the variable -->
<?php else : ?>
<?php
$args = array(
'post_type' => 'events',
'posts_per_page' => 3,
'meta_key' => 'event_start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'ignore_sticky_posts' => true
);
?>
<?php endif; ?>
<?php $secondquery = new WP_Query( $args );
if ( $secondquery->have_posts() ) : while ( $secondquery->have_posts() ) : $secondquery->the_post(); ?>
I'm still wanting to sort by the event date, thus the meta_key and orderby. I'm not sure if that's affecting this. A couple things to note:
• That temporary line echoing the $event_tag does output the tag id (in this case 31).
• I've tried wrapping that tag_id in '', echoing it, etc. But it just displays nothing.
• Because this is querying a custom post type, I'm not sure if the standard tag even works. The tag is registered like this...if it matters.
// Taxonomy / Tags
function taxonomies_events_tags() {
$args = array(
'hierarchical' => false,
'label' => __( 'Event Tags','taxonomy general name'),
'singular_name' => __( 'Event Tag', 'taxonomy general name' ),
'rewrite' => true,
'query_var' => true,
'show_in_rest' => true
);
register_taxonomy( 'custom-tag', 'events', $args );
}
add_action( 'init', 'taxonomies_events_tags', 0 );
What do I need to change in my query to get the events in the specified tag to show, still ordered by event_start_date?
Thanks in advance.
You need to use a tax query to get events from a certain category. Assuming the $event_tag variable contains the tag id for the taxonomy term, the following piece of code should work:
$args = array(
'post_type' => 'events',
'posts_per_page' => 3,
'meta_key' => 'event_start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'ignore_sticky_posts' => true,
'tax_query' => array(
array(
'taxonomy' => 'custom-tag',
'field' => 'term_id',
'terms' => $event_tag
)
)
);

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.

how build a wordpress query_posts based on url

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

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.

Wordpress Content Type in Category.php

Very simple setup, I'm using 1 template for multiple taxonomies. So example:
I have basic category + customtypetaxonomy. Both of them have terms. I'm displaying the posts of these terms in the same template - category.php. Which works great and how I want it.
However, I want to make small changes in this template based on the taxonomie it is getting its posts from. But I cannot figure out what function I need to use to get this.
For example: single_cat_title() gives me the title of the term (category)
But what I need is either the slug or the ID. Is there a way to do this? Something like single_cat_id or single_cat_slug. I have tried $cat but it doesn't give me any output.
Any ideas?
Use WP_Query. Like this:
$ThisQuery = new WP_Query( array( 'category_name' => 'MyCategory',
'meta_key' => 'MyCustomFieldValue',
'posts_per_page' => 10 ) ); // Number of posts
if ( $ThisQuery->have_posts() ) : while ( $ThisQuery->have_posts() ) : $ThisQuery->the_post();
...
endwhile;
endif;
wp_reset_postdata();
Modify the array to include the elements that identify the posts you want to fetch
Another EXAMPLE:
<?php
$Args = array( //Category
'cat' => 5, // Category id.
'category_name' => 'MyCategory', // Category Name.
//Taxonomy
'tax_query' => array( 'relation' => 'AND', // Could be OR
array( 'taxonomy' => 'MyTaxonomy1',
'field' => 'MySlug1',
'terms' => array( 'MyTerm11',
'MyTerm12' ), ),
array( 'taxonomy' => 'MyTaxonomy2',
'field' => 'MySlug2',
'terms' => array( 'MyTerm21',
'MyTerm22',
'MyTerm23' ), ) ), );
$ThisQuery = new WP_Query( $Args );
if ( $ThisQuery->have_posts() ) : while ( $ThisQuery->have_posts() ) : $ThisQuery->the_post();
// Do Stuff
endwhile;
endif;
?>

Resources