How to get the link of the_ID(); - php

I got 4 posts in Wordpress.
In those posts they can add a link from custom field.
I got this code:
$args = array( 'post_type' => 'post');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_ID();
endwhile;
$other_page = 1330; // variable
the_field('bloglink', $other_page); // show link
The output is: 104013301196827, #link
(I made an example for $other_page)
My question is: How can I use those id's to make a link?
Any help is appreciated

get_the_ID() - Retrieve the ID of the current item in the WordPress Loop.
<?php
$args = array( 'post_type' => 'post');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_ID();
the_field('bloglink', get_the_ID()); // show link
endwhile;
?>

Related

Pass custom field into wordpress loop array

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.

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.

Wordpress - Exclude posts from author from wp_query

I'm trying to exclude posts by the author with the ID "1" form WP_Query. This still shows all posts by all users.
Any thoughts?
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query( array( 'author' => -1 ) );
$wp_query->query('posts_per_page=35'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<div class="grid__third">
...
</div>
<?php endwhile; // end of loop
?>
Thanks a lot!
try this
global $post;
$args=array('author' => '-1', //excludes users with id 1.
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 12,
'ignore_sticky_posts'=> 1,);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
or
// The Query
$args=array('author' => '-1', //excludes users with id 1.
'post_type' => 'post','post_status' => 'publish','posts_per_page' => 12,'ignore_sticky_posts'=> 1,);
$the_query = new WP_Query( $args ); // The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();}else {
// no posts found
}
If your query is not working with
$wp_query = new WP_Query( array( 'author' => -1 ) );
then, You can try some other logic. Like inside the loop get the ID of the author by using POST ID:
$post_author_id = get_post_field( 'post_author', $post_id ); // You will get your author id here
Then apply condition like if($post_author_id != 1){ // YOUR POST } This will display post of all author except author with ID = 1.
Full code like:
while ($wp_query->have_posts()) : $wp_query->the_post();
$post_author_id = get_post_field( 'post_author', HERE YOUR POST ID );
if($post_author_id != 1){
?>
<div class="grid__third">
...
</div>
<?php
} endwhile; // end of loop
Hope this will helpful for you. Thanks.

Post_parent function in wordpress does not return page childs

<?php
$currentID = get_the_ID();
$argscases = new WP_Query( array( 'post_parent' => 2431 ) );
$my_query = new WP_Query( array('post_type' => 'page', 'showposts' => '7', 'post__in' => array_diff($argscases, array($currentID))));
?>
<?php
while ( $my_query->have_posts() ) : $my_query->the_post();
?>
<?php the_title() ?>
<?php endwhile; ?>
I am trying to display buttons for each page that is a child of the page with id 2431. And also exclude the button of the current page that I am viewing. But when I execute the code above I get all the pages in return. If anything is unclear I please tell me so I can clarify it.

Custom post type exclude taxonomy posts

I have a cutom post type named : art_design and i have a taxonomy called artdesign_mainslider, in one loop i have only posts from the taxonomy artdesign_mainslider and in the other loop i have posts from custom post type art_design but i want to exclude the taxonomy ?
1st loop:
<?php
$args = array( 'artdesign_mainslider' => 'art-design-featured-post', 'posts_per_page' => 5 );$loop = new WP_Query( $args );while ( $loop->have_posts() ) : $loop->the_post(); ?><?php endwhile; ?>
2nd loop ( i want to exclude artdesign_mainslider):
<?php
$args = array( 'post_type' => 'art_design', 'posts_per_page' => 10, );$loop = new WP_Query( $args );while ( $loop->have_posts() ) : $loop->the_post(); ?><?php endwhile; ?>
You have to use the tax_query parameter. See http://www.wpexplorer.com/exclude-taxonomy-wordpress/ for a perfect example of what you're trying to do and http://codex.wordpress.org/Class_Reference/WP_Query for the reference for tax_query.

Categories