Get taxonomy from one loop and use it in another - php

This code in the first loop:
<?php
$terms_as_text = get_the_term_list( $post->ID, 'produktkategori', '', ', ', '' ) ;
echo strip_tags($terms_as_text);
?>
Results in "Spanskt". I want "Spanskt" to be entered in the second loop where it says "Spanskt". Is that possible?
Thanks in advance.
// First loop
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php
$terms_as_text = get_the_term_list( $post->ID, 'produktkategori', '', ', ', '' ) ;
echo strip_tags($terms_as_text);
?> // The value of this...</h1>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
Some content
</article>
<?php endwhile; endif; ?>
// Second loop
<?php $loop = new WP_Query( array( 'post_type' => 'bocker', 'posts_per_page' => 4, 'produktkategori' => 'Spanskt // ...should be entered here', 'orderby' => 'rand', ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
Some content
<?php endwhile; ?>

This is my solution:
// First loop
<?php $loop = new WP_Query( array( 'post_type' => 'forfattare', 'posts_per_page' => 10, 'orderby' => 'title', 'order' => 'ASC' ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php $string = get_the_title() ;
$forfattare = createAlias( $string );
?>
// Second loop
<?php $loop_2 = new WP_Query( array( 'post_type' => 'bocker', 'posts_per_page' => 10, 'forfattare' => ''.$forfattare.'', 'orderby' => 'rand', 'order' => 'ASC', ) ); ?>
<?php while ( $loop_2->have_posts() ) : $loop_2->the_post(); ?>
<div class="italic"><?php the_title(); ?></div>
<?php endwhile; ?>
<?php endwhile; ?>

Related

wp_reset_query not resetting

I am trying to query several different types of post on the same page. When I try to query the second time (Essays), nothing shows up, meaning my if ($arr_posts->have_posts()) is evaluating as false. The first query and the third and fourth query are working fine. And the second query was working until I added this Interview query before it. And even when I commented it out, it still stopped showing up. What am I missing? And mwp_interview is a custom post type.
<!--Latest Interview-->
<?php
$args = array(
'posts_per_page' => 1,
'post_status' => 'publish',
'post_type' => 'mwp_interview',
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<div>
DISPLAY INTERVIEW POST
</div>
<?php
endif; ?>
<!--Latest Essay-->
<?php
wp_reset_query();
wp_reset_postdata();
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'Essays in Discipleship',
'posts_per_page' => 1
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<div>
DISPLAY ESSAY POST
</div>
<?php
endif; ?>
<!--Latest Special Series-->
<?php
$ss_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'Special Post',
'posts_per_page' => 1,
);
wp_reset_query();
$ss_arr_posts = new WP_Query( $ss_args );
if ( $ss_arr_posts->have_posts() ) :
$ss_arr_posts->the_post();
?>
<div>
DISPLAY SPECIAL SERIES POST
</div>
<?php
endif;
?>
<!--Latest Podcast-->
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'Podcast',
'posts_per_page' => 1,
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<div>
DISPLAY PODCAST
</div>
<?php
endif;
?>
As per the Wordpress documentation WP_Query Category Parameters.
category_name (string) – use category slug.
The argument's name category_name is actually misleading, category_name is referring to the category SLUG, NOT the actual category NAME.
<?php
$args = [
'posts_per_page' => 1,
'post_status' => 'publish',
'post_type' => 'mwp_interview',
];
$query = new WP_Query( $args );
if( $query->have_posts() ):
$i = 0;
while( $query->have_posts() ): $query->the_post();
$i++;
if ( $i > 1 )
echo "<hr>";
the_title( '<h1>', '</h1>' );
endwhile;
else:
echo 'No "interview" just yet!';
endif;
wp_reset_postdata(); ?>
<?php
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'essays-in-discipleship', // ... must be set set to "Essays In Discipleship" category slug
'posts_per_page' => 1,
];
$query = new WP_Query( $args );
if( $query->have_posts() ):
$i = 0;
while( $query->have_posts() ): $query->the_post();
$i++;
if ( $i > 1 )
echo '<hr>';
the_title( '<h1>', '</h1>' );
endwhile;
else:
echo 'No "Essays In Discipleship" just yet!';
endif;
wp_reset_postdata(); ?>
<?php
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'special-post', // ... must be set to "Special Post" category slug
'posts_per_page' => 1,
];
$query = new WP_Query( $args );
if( $query->have_posts() ):
$i = 0;
while( $query->have_posts() ): $query->the_post();
$i++;
if ( $i > 1 )
echo '<hr>';
the_title( '<h1>', '</h1>' );
endwhile;
else:
echo 'No "Special Post" just yet!';
endif;
wp_reset_postdata(); ?>

Multiple Wordpress loops, hide first loop on second page

I've construced two Wordpress loops with numeric pagination as follows:
<div class="container" style="background:#ccc">
<?
$paged2 = isset( $_GET['paged2'] ) ? (int) $_GET['paged2'] : 1;
// Custom Loop with Pagination 1
// http://codex.wordpress.org/Class_Reference/WP_Query#Usage
$args1 = array(
'paged' => false,
'category_name' => 'latest',
'posts_per_page' => 1,
);
$query1 = new WP_Query( $args1 );
while ( $query1->have_posts() ) : $query1->the_post();
the_title();
the_category(' ');
the_excerpt();
endwhile;
?>
<!-- second -->
<?
$args2 = array(
'paged' => $paged2,
'category_name' => 'uncategorized',
'posts_per_page' => 2,
);
$query2 = new WP_Query( $args2 );
while ( $query2->have_posts() ) : $query2->the_post();
the_title();
the_category(' ');
the_excerpt();
endwhile;
$pag_args2 = array(
'format' => '?paged2=%#%',
'current' => $paged2,
'total' => $query2->max_num_pages,
'add_args' => array( 'paged1' => $paged1 )
);
echo paginate_links( $pag_args2 );
?>
</div>
<!-- container -->
Everything works fine, but the paginatin page also shows the 'latest' post loop, although I've set the pagination to false.
How could I hide the first loop on the pagination pages?
Advice appreciated.
use wp_reset_postdata() function after your loop end
follow this code, may be it'll help you
<?php
// example args
$args = array( 'posts_per_page' => 3 );
// the query
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- start of the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endwhile; ?><!-- end of the loop -->
<!-- put pagination functions here -->
<?php wp_reset_postdata(); ?>
<?php endif; ?>

How to make a shortcode for my WP_Query Loop?

I'm a noob Wordpress Developer and I just created my first Custom Template Page using Advanced Custom Fields and managed to loop.
<?php
$args = array(
'post_type' => 'art',
'orderby' => 'title',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', 'art' ); ?>
<?php endwhile; endif; ?>
But I will like to use it not only inside a template page, but anywhere I want. Therefore I need to create a shortcode.
Example:
function foobar_func( $atts ){
return "foo and bar";
}
add_shortcode( 'foobar', 'foobar_func' );
My question would be: How can i put the loop inside my shortcode?
add_shortcode( 'foobar', 'foobar_func' );
function foobar_func( $atts ) {
global $post;
$output = '';
$args = array(
'post_type' => 'art',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => 10,
);
$fe_query= new WP_Query( $args );
if ( $fe_query->have_posts() ) {
$output .= '<ul class="fe-query-results-shortcode-output">';
while ( $fe_query->have_posts() ) {
$fe_query->the_post();
$title = get_the_title();
$link = get_the_permalink();
$output .= "<li>{$title}</li>";
}
$output .= '</ul>';
} else {
$output .= '<div class="fe-query-results-shortcode-output-none">No results were found</div>';
}
wp_reset_postdata();
return $output;
}
<?php
function loop_art() {
ob_start();
get_template_part('loop_art');
return ob_get_clean();
}
add_shortcode( 'loop_art', 'loop_art' );
?>
<?php
$args = array(
'post_type' => 'art',
'orderby' => 'title',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post()
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-meta">
<p>Price: $<?php the_field('price'); ?></p>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<div class="entry-content">
<p><img src="<?php the_field('image'); ?>" alt="Example image of <?php the_title(); ?>"></p>
</div><!-- .entry-content -->
</article>
<?php endwhile; endif; ?>

post_type how to get 1 title

I have done some research on this and couldnt find anything about it.
I got 4 different post.
$args = array(
'post_type'=> 'post',
'order' => 'ASC' );
$the_query = new WP_Query( $args );
if($the_query->have_posts() ) : while $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
endif;
wp_reset_postdata();
this code shows all the titels, the output is like this :
1post2post3post4post
1post2post3post4post
1post2post3post4post
1post2post3post4post
but what i want is this:
1post
2post
3post
4post
How do i get this?
EDIT: Made some changes with the help of #Vincent. How do i get this 2 parts of code to work with each other?
<?php
$args = array(
'post_type'=> 'cubeportfolio',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
if($the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<?php
the_permalink();
endwhile;
endif;
wp_reset_postdata();
?>
<?php
$args = array(
'post_type'=> 'post',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
if($the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<?php
echo get_the_title()."<br/>";
endwhile;
endif;
wp_reset_postdata();
?>
After Edits - try this code
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'order' => 'ASC'
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()) :
?>
<ul>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<li>
<?php the_title(); ?>
</li>
<?php
endwhile;
wp_reset_postdata
?>
</ul>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
EDIT : What to you get with this part only ?
<?php
$args = array(
'post_type'=> 'post',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
if($the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<a href="'.get_the_permalink().'" >'.get_the_title().'</a>'.'<br/>';
endwhile;
endif;
wp_reset_postdata();
?>

How to get child posts of current custom post and order it by custom field number?

I have custom post type 'cars' and its child post type is 'carvariants'.
What I want to do is get child posts (carvariants) of current post (cars). I tried this code:
<div>
<?php
$parent_id = 1064;
$the_query = new WP_Query(array(
'post_parent' => $parent_id,
'post_type' => 'carvariants',
'posts_per_page' => 1,
'meta_key' => 'wpcf-minimum-price',
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post();
$compprd = get_the_ID(); ?>
<?php the_title(); ?>
<?php
endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
I want to display child posts of Cars order by custom field wpcf-minimum-price
but 'post_parent' is not working. This code is showing blank output. Whats wrong in this?
I didn't try this. But I hope this will work.
If it will not work, leave me a comment, and I will try to make it work.
Also, if there are better solutions, I will be glad to see the code from professionals:
<div>
<?php
$parent_id = 1064;
$args = array( 'child_of' => $parent_id );
$children_pages = get_pages( $args );
if ( count( $children_pages ) != 0 ) :
foreach ( $children_pages as $children_page ) :
if ( $children_page->have_posts() ) :
$args_for_posts = array( 'posts_per_page' => 1,
'post_type' => 'carvariants',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'post_parent' => $children_page );
$postlist = get_posts( $args_for_posts );
foreach ( $postlist as $post) :
setup_postdata( $post ); ?>
<ul>
<?php
the_post();
?>
</ul>
<?php
endforeach;
wp_reset_postdata();
endif;
endforeach;
else : ?>
<p>No content to show.</p>
<?php
endif; ?>
</div>

Categories