Wordpress get_pages based on page category - wordpress

I need to get_pages or get an array of pages in WP based on it 's category
I know WP doesn't come with categories on pages but I'm using this in the functions.php to get categories on the pages.
add_action('admin_init', 'reg_tax');
function reg_tax() {
register_taxonomy_for_object_type('category', 'page');
add_post_type_support('page', 'category');
}
Now I need to use these categories in get_pages or WP_Query to get in the pages with a category.
<div class="productNav">
<ul>
<?php
$product_page_args = array(
'post_type' => 'page',
'order' => 'ASC',
'orderby' => 'menu_order',
'child_of' => $post->ID,
'category_name' => 'pillar-product'
);
//$product_pages = get_pages($product_page_args);
$product_pages = new WP_Query($product_page_args);
foreach ($product_pages as $product_page){
?>
<li><?php echo $product_page->post_title; ?></li>
<?php
}
?>
</ul>
</div>

Respectfully this answer does NOT work anymore in 2021. This is an obsolete answer and includes all of the pages. Please use the other answer.
Try this: (won't work on wordpress 5.8)
$product_page_args = array(
'post_type' => 'page',
'order' => 'ASC',
'orderby' => 'menu_order',
'child_of' => $post->ID,
'taxonomy' => 'category',
'field' => 'slug',
'term' => 'pillar-product'
);

The accepted answer for this question won't work anymore and includes all of the pages because 'taxonomy', 'field' and 'term' must be inside an array which resides inside 'tax_query' array, for this query to work.
For more comprehensive answer on how to query wordpress pages based on specific category see the following link:
How to query pages based on specific categories
Here's the correct answer for this question, fully tested on wordpress 5.8.
$product_page_args = array(
'post_type' => 'page',
'order' => 'ASC',
'orderby' => 'menu_order',
'child_of' => $post->ID,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array('pillar-product'),
)
)
);

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

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

how to call optiontree taxonomy select

I ask for help to solve my problem, I use the option-tree framework for displaying custom posts from specific categories, I have this code:
<?php
global $product;
$category_product1 = ot_get_option( 'homepage-slider-cat1' );
$args = array(
'post_type' => 'product',
'numberposts' => 18,
'post_status' => 'publish',
'category' => $category_product1,
'include_children' => true
);
?>
with this post code specific category does not appear.
Please help me
Have you tried the Tax_query as a query parameter? This way you can query on taxonomies.
'tax_query' => array(
array(
'taxonomy' => 'people',
'field' => 'slug',
'terms' => 'bob',
),
),
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

Wordpress output links to previous and next posts from custom query

I'm using the advanced custom fields plugin for wordpress to create a group of custom post types that have a date set within them.
I'm trying to show the previous post, and the next post, based on the date stored in the custom field. The links need to link to posts that have a date set in the future (so don't show links to posts with dates that have gone by)/
I can get a list of all the posts that are in the future, and out put these using the following code;
<?php
$rightnow = current_time('Ymd');
$args = array(
'post_type' => 'Courses',
'posts_per_page' => '25',
'meta_query' => array(
array(
'key' => 'date_of_the_course_single_day',
'compare' => '>=',
'value' => $rightnow,
)
),
'meta_key' => 'date_of_the_course_single_day',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish'
);
$posts = get_posts($args);
foreach ( $posts as $post ) {
?>
Output details of post here....
<?php
}
?>
What I thought I could do, is the get the current post's position in the array, to then get details of the posts one before and one after... but I haven't got a clue how to do this.
I've experimented with the wordpress next_post_link and previous_post_link functions, but these seem to work based on when the post was added to wordpress, rather than based on my custom date field.
Am I going about this the complete wrong way? Any tips or pointers would be much appreciated!
Use WP_Query plus paginate_links
$rightnow = current_time('Ymd');
// Query Args
$args = array(
'post_type' => 'Courses',
'posts_per_page' => '25',
'meta_query' => array( array(
'key' => 'date_of_the_course_single_day',
'compare' => '>=',
'value' => $rightnow,
) ),
'meta_key' => 'date_of_the_course_single_day',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish'
);
$query = new WP_QUery( $arg );
$posts = $query->get_posts();
// Paginate Args
$page_args = array(
'base' => 'your_custom_page_url'.'%_%', // Make sure you got this current depending on your setup
'format' => '/%#%', // requires pretty permalinks
'total' => $query->max_num_pages,
'current' => 0,
'prev_text' => __('«'),
'next_text' => __('»'),
);
foreach ( $posts as $post ) {
// Output
}
echo paginate_links( $page_args );
You have to verify that the base and format of paginate args are correct of it won't properly worked.

Query posts by custom taxonomy ID

I have a custom post type called portfolio and a custom taxonomy called build-type (acting as categories)
I am trying to query portfolio posts by build-type ID e.g. all Portfolio posts in "Hotels" (id=4 for that taxonomy)
// gets the ID from a custom field to show posts on a specific page
$buildType = get_post_meta($post->ID, 'build_type_id', true);
// run query
query_posts(array(
'post_type' => 'portfolio',
'showposts' => -1,
'tax_query' => array(
'taxonomy' => 'build-type',
'terms' => $buildType,
'field' => 'term_id'
),
'orderby' => 'title',
'order' => 'ASC'
));
Currently it's calling all portfolio posts and not just those with the build-type ID
For 'field' => 'term_id' should I be using term_id, tag_ID, id or something else?
Anyone know how to get this working?
Thanks in advance!
I solved it with help from: https://wordpress.stackexchange.com/questions/30476/query-posts-by-custom-taxonomy-id
tax-query needs to be an array of arrays
The final solution is:
// gets the ID from a custom field to show posts on a specific page
$buildType = get_post_meta($post->ID, 'build_type_id', true);
// run query
query_posts(array(
'post_type' => 'portfolio',
'showposts' => -1,
'tax_query' => array(
array(
'taxonomy' => 'build-type',
'terms' => $buildType,
'field' => 'term_id',
)
),
'orderby' => 'title',
'order' => 'ASC' )
);
On github here:
https://gist.github.com/1275191
I'm not a WP-gury and I have invested hours and hours trying to solve the same problem. Eventually I found this blog post: http://richardsweeney.com/blog/wordpress-3-0-custom-queries-post-types-and-taxonomies/
The answer is somewhat semi-bad: apparently you can't filter like this for custom post types (it is only possible for posts), which is a shame!
What I did work was this:
$args['custom_tax'] = 'custom_tax_slug';
query_posts($args);
Hope it helps!
//Mike

Resources