I want to echo the pages only with the custom taxonomy, but now i have drop down all pages what i have in custom post type.
<?php
$post_type_object = get_post_type_object('property');
if ($post) {
$parent_properties_dropdown_args = array(
'post_type' => 'property',
'property_status' => 'condominium-villas-project', // <<-- Here is the (property_status - is the custom taxonomy and condominium-villas-project is the custom taxonomy tag)
'selected' => $prop_data->post_parent,
'name' => 'parent_id',
'show_option_none' => __('Not selected'),
'echo' => 0,);
$parent_properties_dropdown = wp_dropdown_pages($parent_properties_dropdown_args);
if (! empty($parent_properties_dropdown)) {
?>
<div class="form-option parent-field-wrapper">
<label for=""></label>
<?php echo $parent_properties_dropdown; ?>
</div>
<?php
} }
But im anyway get all pages in custom post type 'property'. i need only 'property' with the taxonomy to show.
example:
now ->
- Property 1 - 'property_status' 'rent'
- Property 2 - 'property_status' 'sale'
- property 3 - 'property_status' 'condominium-villas-project'
i want to get only
- property 3 - 'property_status' 'condominium-villas-project'
Using what I think is your exact code (I am still confused by your use of $prop_data) you could use the following code to get what (I think) you are looking for. Add the code to your theme's function.php file, a .php file it requires, or one of the .php files of a plugin you might implement:
<?php
class wpse_51782342 {
static function on_load() {
add_filter( 'get_pages', [ __CLASS__, '_get_pages' ], 10, 2 );
}
static function _get_pages( $pages, $args ) {
if ( isset( $args[ 'property_status' ] ) ) {
$pages = get_posts(array(
'post_type' => 'property',
'posts_per_page' => -1,
'property_status' => $args[ 'property_status' ],
'include' => wp_list_pluck( $pages, 'ID' ),
));
}
return $pages;
}
}
wpse_51782342::on_load();
The 'get_pages' filter hook runs at the end of get_pages() which is called by wp_dropdown_pages(). In that hook the code looks for the argument you passed named 'property_status' to decide if it should modify behavior. This is an important technique because it ensures that the same args will always return the same results and are not dependent on something like the current post ID or URL. Following this principle will usually reduce the number of bugs you have to fix in your project.
If the argument 'property_status' is found the $args array the code uses its value to call get_posts() to return a list of posts that have been assigned the value of property_status that you passed to wp_dropdown_pages().
Finally the code limits the get_posts() query to the $post->IDs from $pages found by the query already run by wp_dropdown_pages(). This should result in a dropdown showing just the pages you prefer.
And for reference, here is the code in single.php to test out the above code, after I entered examples for property and property status, of course.
wp_dropdown_pages(array(
'post_type' => 'property',
'property_status' => 'condominium-villas-project',
'selected' => $post->ID,
'name' => 'ID',
'show_option_none' => __('Not selected'),
'echo' => 1,
));
Hope this helps?
You cannot filter wp_dropdown_pages() with custom taxonomy. You can use normal WordPress query like below.
<?php
$the_query = new WP_Query( array(
'post_type' => 'property',
'tax_query' => array(
array (
'taxonomy' => 'property_status',
'field' => 'slug',
'terms' => 'condominium-villas-project',
)
),
) );
if ( $the_query->have_posts() ) :
?>
<div class="form-option parent-field-wrapper">
<label for=""></label>
<select name='parent_id' id='parent_id'>
<option value="">Not selected</option>
<?php
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<option value="<?php the_ID(); ?>"><?php the_title(); ?></option>
<?php endwhile; ?>
</select>
</div>
<?php
endif;
wp_reset_postdata();
?>
Related
Hi I have created my own custom post type within Wordpress to contain projects that i can call via my theme files.
I am new to creating my own themes. I currently am using the following code in my single.php file to call in related articles based on the category of the blog post.
<?php
// Default arguments
$args = array(
'posts_per_page' => 3, // How many items to display
'post__not_in' => array( get_the_ID() ), // Exclude current post
'no_found_rows' => true, // We don't ned pagination so this speeds up the query
);
// Check for current post category and add tax_query to the query arguments
$cats = wp_get_post_terms( get_the_ID(), 'category' );
$cats_ids = array();
foreach( $cats as $wpex_related_cat ) {
$cats_ids[] = $wpex_related_cat->term_id;
}
if ( ! empty( $cats_ids ) ) {
$args['category__in'] = $cats_ids;
}
// Query posts
$wpex_query = new wp_query( $args );
// Loop through posts
foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<div class="col-md-4 related-post">
<?php the_post_thumbnail('large'); ?>
<?php the_title(); ?>
</div>
<?php
// End loop
endforeach;
// Reset post data
wp_reset_postdata(); ?>
In my new post type "projects" i would like to call in related projects. Which im assuming would be very similar code except i need to stop it looking for posts and instead look for my projects.
Here is my code for new post type:
// Projects
add_action( 'init', 'create_post_type' );
add_post_type_support( 'bw_projects', 'thumbnail' );
add_post_type_support( 'bw_projects', 'custom-fields' );
function create_post_type() {
register_post_type( 'bw_projects',
array(
'labels' => array(
'name' => __( 'Projects' ),
'singular_name' => __( 'Projects' )
),
'public' => true,
'has_archive' => true,
'taxonomies' => array( 'category','post_tag'),
)
);
}
What would i need to change in my first code snippet in order to look for bw_projects and not look for 'posts' anymore. I tried playing around and changing certain lines myself but i caused more issues and stopped the page loading. Is this even right i can use the same code, slightly altered or would i need something completely different?
Thanks in advance.
You can get any post type that you require using get_posts();
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'projects',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
Simply set the 'post_type' argument to that of you custom post type, to get only these posts. You can also set the number of post, and filter by category etc.
You can find more info in the codex.
Alternatively, if you wanted to keep something similar to your existing code you could try using 'pre_get_posts' to filter the query to just your projects. However you'd need to remember to add / remove this filter so it only operates on the queries that need it.
To display the posts you can use a simple foreach to churn them out. You#d obviously want to do some sort of styling to get the layout correct:
$args = array("posts_per_page" => 10, "orderby" => "comment_count");
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
echo "<h1>" . $post->post_title . "</h1><br>";
echo "<p>" . $post->post_content . "</p><br>";
}
Or a really concise way of doing all of the above would be something like:
$args = array("posts_per_page" => 5, "post_type" => "projects");
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
echo "<h1>" . $post->post_title . "</h1><br>";
echo "<p>" . $post->post_content . "</p><br>";
}
I have two custom posts types A and B, linked together with a same custom taxonomy.
While looping through A posts with the "default" loop, I want for each A to get all B with the same taxonomy.
The code looks like this:
<?php if(have_posts()): while(have_posts()): the_post(); ?>
<?php
$A_Bs=get_the_terms( $post->ID, 'A_B');
?>
<?php if($A_Bs!=false && count($A_Bs)>0):?>
<?php
$A_B=$A_Bs[0];
$args = array(
'post_type' => 'B',
'tax_query' => array(
array(
'taxonomy' => 'A_B',
'field' => 'term_id',
'terms' => $A_B->term_id,
),
),
);
$loop = new WP_Query($args);
$saved_post=$post;
?>
<?php while ($loop->have_posts()) : $loop->the_post();?>
blabla
<?php endwhile;?>
<?php $post=$saved_post;?>
<?php endif;?>
<?php endwhile; endif;?>
But the sub-loop is always empty. The reason is, in the query_vars I have these two guys:
'meta_key' => string 'position' (length=8)
'orderby' => string 'meta_value_num' (length=14)
and I can't get rid of them. I never specified this ordering anywhere and my B posts don't have this custom field.
It's generating this line in the SQL query:
aaaa_postmeta.meta_key = 'position'
and prevent me to list the posts.
I tried to play with the $args, removing the tax_query and changing the post_type but it's always the same.
Thank you for your time !
Sorry I just realized after hours that I have the following thing in functions.php
function order_by_position($query){
if(is_post_type_archive( 'A')||is_post_type_archive( 'C')||is_post_type_archive( 'D')){
$query->query_vars['meta_key']="position";
$query->query_vars['orderby']="meta_value_num";
}
return $query;
}
add_action( 'pre_get_posts', 'order_by_position' );
It's much more logical now.
Sorry for disturbing.
I would like to find out what is the way to filter posts of a specific category using custom fields. The result I want is exactly the same as the table here
. I have created a list like that and inside the table cells I have inserted three different custom fields each one containing a number of values. How can I use the same structure as above the list of forbes magazine and filter posts using drop down menus?
I think this documentation is pretty clear https://codex.wordpress.org/Class_Reference/WP_Meta_Query.
However i am not quite sure what you are asking for.
do you want to filter the HTML output or your wordpress database by custom field??
btw, if you want to filter the html output, then use jQuery, i used to filter over 2000 List in Table row using data-attribute;
Here is the jQuery Documentation, https://api.jquery.com/category/selectors/
if you want to filter and reorder the wordpress table using the meta value
To order the by meta value
$args = [
'meta_key' => '*meta_keyword*',
'orderby' => 'meta_value',
'order' => 'ASC'
];
meta_keyword need to change based your on meta_key, you can order them pretty much by every type of value, Date, int etc
to filter the table
$args['meta_query'] = [
[
'key' => 'meta_key',
'value' => 'filter_value',
'type' => 'str*',
'compare' => '=*'
]
];
'*' Depend on value type and your logic
Here some information
https://wordpress.stackexchange.com/questions/30241/wp-query-order-results-by-meta-value
Another answer
https://stackoverflow.com/a/24253081/3392555
Please find the similar example on my blog. Here I am filtering using the custom fields and category name and displaying those as a block in the home page.
http://www.pearlbells.co.uk/filter-posts-custom-fields-wp_query/
$args = array(
'category_name' => 'courses',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'front_page',
'value' => 'yes',
'compare' => 'LIKE',
))
);
$the_query = new WP_Query( $args );
Custom Post type name = Software,
Custom field name = version meta
This coding is done on single.php. $version is used for call the custom field value on single.php and filter the data by custom field value.
<?php $args = array('post_type' => 'software', 'showposts'=>'4', 'category_name' => '', 'orderby' => '','meta_query' => array(array('key' => 'version_meta','value' => $version = get_post_meta($post->ID, 'version_meta', true))));
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();?>
<div class="post_desc">
<div class="thumb thmbhome3">
<?php the_post_thumbnail();?>
</div>
<div class="title_des edu tthome3">
<div class="title edu_titl ttshome3">
<h1><?php the_title();?></h1>
</div>
</div>
</div>
<?php endwhile; ?>
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
I created a field with the Adv Custom Fields plugin which allows the user to select which section the page is under (like categories). On each page I'd like to display a sidebar which shows a list of pages with the same section. I attempted to use meta_query and I don't get any results. I would also like to display the parent page first if there's a way to do it. Here's my query:
<ul class="test-menu">
<?php
$section = get_field('section');
$args = array(
'meta_query' => array(
array(
'key' => 'section',
'value' => $section
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
Seems like you need to specify a post_type in your query and you are missing the compare bit although I'm not sure which one throw you off :
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'section',
'value' => $section,
'compare' => "="
)
)
);
The post type can probably be an array if you have multiple type of custom posts.