Wordpress nested loop, cannot remove meta_key from the query - php

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.

Related

Limit excerpts of post types

I have two post types, the regular posts and a custom post type. Everything is working fine and I show only 5 posts. One as a full post and four as excerpts. The problem I have is that the excerpts is showing the latest posts, independent of post category. I want to show two of the posts and two of the custom post type.
$args = array(
'post_type' => array( 'post', 'tutorial' ),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
$count = 0;
while ( $query->have_posts() ) : $query->the_post();
if ( $count == 0 ) { ?>
<h2><?php the_title(); ?></h2>
<?php the_content();
$count ++;
} else { ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt();
}
endwhile;
endif;
wp_reset_postdata();
?>
Expected output should be the latest post as a full post, as it is working now. Then it should display the two latest posts of post type post and two latest posts of post type tutorial.
Basicly you only need to sort by posttype
$args = array(
'post_type' => array( 'post', 'tutorial' ),
'orderby' => 'post_type',
'order' => 'ASC',
);
If you want to keep the sorting of date as a secondary sort this should work (not tested).
$args = array(
'post_type' => array( 'post', 'tutorial' ),
'orderby' => array ('post_type' => 'ASC', 'order' => 'DESC' ),
);
For more information check the WP_Query documentation
Keep in mind that if you have 5 posts newer then any of your tutorials, none will show.
To guarantee 3 posts and 2 tutorials you will need to split the code in 2 wp_query loops with the posts_per_page parameter.

wp_dropdown_pages (Show pages only with custom taxonomy)?

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

WordPress - Code to display post of certain category and display a custom field

I've done hours of Google searching but still stumped on where to start.
I'm trying to display a grid of posts in a certain category, whilst also grabbing and displaying a custom field in that post.
I just need a starting point, then I can figure out how to implement it and then style it.
Any help would be greatly appreciated!
I think you could try this code:
$args = array(
'post_type' => 'medlem',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => 4
)
)
);
As 'field' => 'term_id' is default you can skip this line. For more information go to: http://codex.wordpress.org/Class_Reference/WP_Query
You can display a list of post in the grid by using category id in an array with a query like below:
<?php global $post;
$args = array( 'posts_per_page' => -1, 'offset'=> 1, 'category' => array(1,2,4) );
foreach ( $myposts as $post ) : setup_postdata( $post );
$custom_field = get_post_meta($post->ID, 'your_key', true); // TO get custom field value of post.. ?>
<?php the_title(); ?>
<?php endforeach;
wp_reset_postdata(); ?>
You can also get custom field value by using get_post_meta function with post id like mentioned above code.
Hope this will helpful for you. Thanks.

Wordpress - Increase amount of posts to be shown within specific category

I've been running in to a problem.
I'm editing a custom wordpress theme. I want to make some adjusments to the FAQ page (created with custom post type).
Right now every subject of question (= also category) is showing only 5 answers (these are posts).
I wanted to know, how I could increate this number, so instead of 5, show 10 or rather, show all answers per subject.
This is the code:
archive-faq.php
<?php $terms = get_terms( 'faq_category' ); ?>
<?php foreach( $terms as $term ):
$args = array (
'post_type' => 'faq',
'tax_query' => array(
array(
'taxonomy' => 'faq_category',
'field' => 'id',
'terms' => $term->term_id,
),
),
);
$query = new WP_Query( $args ); ?>
<h1><?php echo $term->name; ?></h1>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="toggle-holder">
<h5 class="toggle"><span><?php the_title(); ?></span></h5>
<div class="toggle-box">
<div>
<?php the_content(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
An important part is:
<div class="toggle-box">
<div>
<?php the_content(); ?>
</div>
</div>
Where the_content() , is showing the posts that are in a certain category.
However it's nowhere to be found why , the page is only showing up to 5 posts (answers) and not any more ?
Things I've tried:
$query = new WP_Query( $args );
$query_posts('post_per_page=3'); ?>
Also:
putting 'showposts' => 8 under 'terms' => $term->term_id,
And I also tried:
$query = new WP_Query( $args ); ?>
<?PHP
query_posts( array(
'workcap' => $all_post_terms,
'showposts' => 8,
'caller_get_posts' => 1,
'post__not_in' => $do_not_duplicate ) ); ?>
->> In summary:
Why does the page only show up to 5 posts ?
And how do I change this property ?
PS. If you want to see the page: http://goo.gl/UnWRTz
The argument is posts_per_page and not post_per_page. Its a typo?
Try this:
$args = array (
'post_type' => 'faq',
'posts_per_page' => -1,
//for now try without this taxonomy, if it works try with it.
/*'tax_query' => array(
array(
'taxonomy' => 'faq_category',
'field' => 'id',
'terms' => $term->term_id,
),
),*/
);
Btw, you can find more info here: https://codex.wordpress.org/Class_Reference/WP_Query

Wordpress meta_query not displaying results

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.

Categories