Pass custom field into wordpress loop array - php

I've got an advanced custom, outputting a chosen taxonomy, which I'm trying to pass into an array inside a WordPress loop.
The loop, which shows a specific taxonomy of my custom post type, is here:
<?php
$loop = new WP_Query( array(
'post_type' => 'portfolio',
'portfolio_category' => 'social-media-marketing',
'posts_per_page' => -1,
)
);
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; wp_reset_query(); ?>
I would like to replace the portfolio category with the results from the custom post type, so the user can select which taxonomy to display.
The code I have to pull in the Advanced custom field taxonomy is here:
<?php
$term = get_field('portfolio_category');
if( $term ): ?>
<h2><?php echo $term->slug; ?></h2>
<?php endif; ?>
Both bits of code work separately. I've tried running them both together like this:
<?php
$term = get_field('portfolio_category');
$loop = new WP_Query( array(
'post_type' => 'portfolio',
'portfolio_category' => 'echo $term->slug;',
'posts_per_page' => -1,
)
);
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; wp_reset_query(); ?>
As well as a few other things, but I can't seem to get it to display anything... What am I doing wrong??

Change this
'portfolio_category' => 'echo $term->slug;'
To:
'portfolio_category' => $term->slug
You were passing the variable as a string instead of a variable.

Related

How can I show specific posts on a WordPress page?

I have a page where I want to reference the titles from specific posts. This is my code with a loop right now -
<?php
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'cat' => '3',
);
$product_posts = get_posts( $args );?>
<p>
<?php foreach ( $product_posts as $post ) : setup_postdata( $post ); ?>
<?php echo get_the_title(); ?>
</p>
<?php endforeach; ?>
I don't want to loop through every post though, I want to be able to single out certain posts. For example, where I have the <p> get_the_title </p> I want to be able to display it like -
<p>Title of Post 5 vs Title of Post 6</p>
How can I do this?
You can try with below:
On the $catquery query the cat=3 is category ID so you can change with your specific category ID. And post_per_page=5 is total count of post so also you can change as per your required.
<?php $catquery = new WP_Query( 'cat=3&posts_per_page=5' ); ?>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; wp_reset_postdata(); ?>
Thanks and let me know if any query.
Try this with post ID
$postid= array(144, 246);
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'post__in' => $postid,
'posts_per_page'= 5
);
// The Query
$the_query = new WP_Query( $args );
<?php while($the_query->have_posts()) : $the_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; wp_reset_postdata(); ?>

Filter & display Custom Post Type posts by their assigned catagories?

I have a Custom Post Type called 'Services', it's slug is called 'services'. And as displayed in this image, they all have their own assigned categories.
How do I go about writing a WP_Query/loop that displays the titles of all 'Services' with the category 'Face'? (With a hyperlink to their respective pages also.)
Below is code that will achieve the result you want
$args = array(
'post_type' => 'services',
'category' => 'Face',
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
----Here will be your html in which format you want to display your title-----
<p><?php the_title(); ?></p>
<?php endwhile;
endif; ?>

Iterating over correct number of wordpress posts from category + custom post type

Our wordpress post loop combines and displays posts from a specific category and a custom post type. This works, but is not displaying all posts. I believe that the post loop is iterating over the number of posts in the specific category, not the number of posts in the specific category + the number of posts in the custom post type. How can I ensure that the correct number of posts are being displayed?
<?php
/*
Template Name: Articles & Cases
*/
get_header(); ?>
<div class="center-holder">
<div id="content">
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title( '<h1>', '</h1>' ); ?>
<?php the_post_thumbnail( 'full' ); ?>
<?php the_content(); ?>
<?php if ( $cats = get_field( 'category' ) ) : ?>
<?php
$args = array(
'post_type' => array( 'post' ),
'category__in' => $cats,
'fields' => 'ids',
);
$articles = new WP_Query( $args );
wp_reset_postdata();
$args = array(
'post_type' => array( 'case_study' ),
'fields' => 'ids',
);
$case_study = new WP_Query( $args );
wp_reset_postdata();
$all_posts_ids = array_merge( $articles->posts, $case_study->posts );
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => array( 'post', 'case_study' ),
'post__in' => $all_posts_ids,
'paged' => $paged,
);
query_posts( $args );
?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'blocks/content', get_post_type() ); ?>
<?php endwhile; ?>
<?php get_template_part( 'blocks/pager' ); ?>
<?php else: ?>
<?php get_template_part( 'blocks/not_found' ); ?>
<?php endif; wp_reset_query(); ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
<?php get_sidebar( 'blog' ); ?>
</div>
<?php get_footer(); ?>
There is a number of things you're doing wrong.
You are using a page template, and then are looping using the loop.
This probably pulls all posts, as the last fallback for page template is index.php (as seen in the diagram here).
Then you are making 3 additional queries while in the loop (so for every post you loop you make 3 extra queries).
And the last query, you are using query_posts() which is overriding the main query. Just don't ever use that.
So the plan of attack should be:
What do I want to show?
How and where do I want to show it?
What do I need to do to achieve this?
Start writing things needed to achieve this.
???
Profit!!!
If you want to control specific taxonomy, use $taxonomy.php template (with $taxonomy being the name of the taxonomy.).
Hope this helps.

Show related posts by taxonomy Wordpress

I am trying to show related posts by taxonomy in single post page on Wordpress. I've used the following code to show posts in the same category, but not the same custom taxonomy. The custom taxonomy I need to use is product_cat
<?php
global $post;
$categories = get_the_category();
$category = $categories[0];
$cat_ID = $category->cat_ID;
$loop = new WP_Query( array( 'post_type' => 'product','post__not_in' => array($post->ID), 'category' => $cat_ID ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title( '<h2 class="entry-title">', '</h2>' ); ?>
<?php endwhile; ?>
How can I adjust the current code?
Did you tried adding the following argument to your WP_Query array of arguments?
'tax_query' => array(array('taxonomy' => 'product_cat'))
The code will look like this (of course after removing the category parameter):
<?php
global $post;
$loop = new WP_Query( array( 'post_type' => 'product','post__not_in' => array($post->ID), 'tax_query' => array(array('taxonomy' => 'product_cat'))) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title( '<h2 class="entry-title">', '</h2>' ); ?>
<?php endwhile; ?>

Only returning parents in the wordpress loop

I am using a custom post type in my wordpress theme, and I need help with the loop. Here is my code:
<?php $loop = new WP_Query( array( 'post_type' => 'magazine', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<?php the_post_thumbnail( 'magazine' ); ?>
<h2><?php the_title( ); ?></h2>
<?php the_content;?>
</li>
<?php endwhile; ?>
This returns the 10 latest posts in the custom field "magazine". I want it to only display the parents in the custom field "magazine". Just like pages, my custom fields have attributes, so you can select a hierarchy (parent/child). I want to edit the loop so it only returns the parents (the latest issues of the magazine, not the articles within each issue) Does anyone know how to do that using the wordpress loop above?
Just add 'post_parent' => 0 to the args array.
<?php $loop = new WP_Query( array( 'post_type' => 'magazine', 'posts_per_page' => 10, 'post_parent' => 0 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<?php the_post_thumbnail( 'magazine' ); ?>
<h2><?php the_title( ); ?></h2>
<?php the_content;?>
</li>
<?php endwhile; ?>

Categories