Wordpress Custom Post Type PHP - php

I have a Portfolio on my website built with Wordpress custom post types and Advanced custom fields - I currently have this bit of code, looping through the 'brand' category of my portfolio:
<?php
$portfolio_args = array(
'post_type' => 'portfolio',
'portfolio-category' => 'brand',
'posts_per_page' => -1
);
$portfolio = new WP_Query($portfolio_args);
while($portfolio->have_posts()) {
$portfolio->the_post();
$post = new SeedPost(get_the_ID());
$post->display(true);
}
wp_reset_query();
?>
This works fine, but I would like to bring in the category name dynamically from a custom field... So, this is the line I think I should edit...
'portfolio-category' => 'brand',
I have tried the following two options but neither seem to work:
'portfolio-category' => '<?php the_field('category_to_show'); ?>',
'portfolio-category' => 'the_field('category_to_show');',
I understand the first option probably doesn't work because I have more PHP tags in there so I removed them for the second attempt - Still isn't working though - Can anyone help?

You should be able to get what you need like this:
$portfolio_args = array(
'post_type' => 'portfolio',
'portfolio-category' => get_field('category_to_show'),
'posts_per_page' => -1
);
The get_field() function returns the value of the specified field.
Hope this helps...
Ref: https://www.advancedcustomfields.com/resources/get_field/

Related

Getting custom post type data including ACF with Timber?

I need to get custom post type data for usage with Timber and Twig, it's working fine using the standard WP_Query, such as:
// Get the testimonials custom post type posts
$args = array(
'post_type' => 'testimonial',
'post_status' => 'publish',
'perm' => 'readable',
'nopaging' => true
);
$context['testimonials'] = new WP_Query( $args );
// Restore the global $post to the current post in the main query
wp_reset_postdata();
However this obviously doesn't get ACF fields.
Was going to do the below as indicated in the Timber docs:
// Get the testimonials custom post type posts
$args = array(
'post_type' => 'testimonial',
'post_status' => 'publish',
'perm' => 'readable',
'nopaging' => true
);
$context['testimonials'] = Timber::get_posts( $args );
However, it seems that get_posts is getting deprecated in 2.0.
It seems like the best way to do it would be by using Timber's PostQuery, but because of lacking documentation I am unsure if it is more or less an encapsulation of WP_query and I can simply do something like:
// Get the testimonials custom post type posts
$args = array(
'post_type' => 'testimonial',
'post_status' => 'publish',
'perm' => 'readable',
'nopaging' => true
);
$context['testimonials'] = new PostQuery(array('query' => $args));
Am I on the right track here or what is the correct way?
Ok, after finding this answer and also digging through the code a bit I found out how to do it I believe:
// Get the testimonials custom post type posts
$args = array(
'post_type' => 'testimonial',
'post_status' => 'publish',
'perm' => 'readable',
'nopaging' => true
);
$query = new Timber\PostQuery($args);
// Get an array of post objects
$context['posts'] = $query->get_posts();
From what I can tell, all the respective WP_Query args seems to work using this method as well - but if you need pagination to work I would advise reading that other answer I linked to as it contains more information on that.
ACF fields have some weird numerical keys in the database. To get the data just use their own function
get_fields();
https://www.advancedcustomfields.com/resources/get_fields/

Find tags by custom field value - Wordpress

I'm customizing the admin of wordpress and I've created a new custom input field for the tags of a post.
It's a dropdown list with all the categories of a post. Now, I wanna find all the tags with an specific category. So, I'm using this:
$args = array(
'meta_key' => 'project',
'meta_value' => $idProject,
);
$allTags = get_tags( $args );
Is this the right way to get all the tags that I want? The problem is that the array list with the tags is coming with only one result. It should bring 2 tags with the project id that I'm passing on the variable $args.
Am I missing something?
I found a solution:
$args = array(
'hide_empty' => false, // also retrieve terms which are not used yet
'meta_query' => array(
array(
'key' => 'project',
'value' => $idProject
)
)
);
$terms = get_terms( 'post_tag', $args );

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.

Filter WordPress posts with custom fields

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

Hide specific posts from category list in wordpress

I'm having some trouble displaying a list of posts from a Wordpress Category that will exclude a certain number of post based on a custom field using Advance Custom Fields.
Here's the current code I'm using that hides it nicely:
while ( have_posts() ) : the_post();
$is_taken = get_field('taken_check', $this_id);
if ($is_taken!=1) {
get_template_part( 'basket_selection' );
}
endwhile;
However, it simply just hides the post but still considers it as a post on the "posts_per_page" function.
For example, There are 20 posts in total and I've set the limit to 10 posts per page. If I hide 3 posts with the code above, it will only display 7 posts in page 1 and 10 posts in page 2.
Is there a way to simply just ignore the hidden posts and not count it as a "post"?
Try this:
Apply Custom Fields Parameters in get_post query itself.
$posts = get_posts(array(
'posts_per_page' => 10,
'post_type' => '<YOUR_POST_TYP>',
'meta_key' => 'taken_check',
'meta_value' => '<DEFAULT_VALUE_OF_taken_check>'
));
Lots to read here: http://codex.wordpress.org/Template_Tags/get_posts
I've managed to solve it by changing the get_posts to wp_query within the category.php.
I first added this code to detect the current category viewed and filter the query to only display taken_check = 0.
$this_cat = get_category(get_query_var('cat'), 'ARRAY_A', false);
foreach ($this_cat as $this_cat){
$this_catid = $this_cat;
break;
}
$args = array(
'posts_per_page' => 10,
'post_type' => 'post',
'cat' => $this_catid,
'orderby' => 'title',
'order' => 'ASC',
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'taken_check',
'value' => '0',
)
)
);
$wp_query = new WP_Query($args);
I then just continued with the default loop sequence. The only weird code is the unnecessary foreach loop to detect the current category based on the current page and not from a post. Still puzzled as to why I can't just use $this_cat[0] since it's an array. It keep returning blank.
Oh well, but it works now with pagination, so I'm happy :)
Thanks for all the help!

Categories