Query post using shortcode - php

I am using this query for getting event custom posts
<?php
global $post;
$args = array( 'posts_per_page' => -1, 'post_type'=> 'events' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<h2><?php the_title(); ?></h2>
<p><?php echo get_post_meta($post->ID, 'events_photo', true); ?></p>
<?php endforeach; ?>
Now I want to be create a shortcode for this query. I am using this code
function get_events(){
global $post;
$args = array( 'posts_per_page' => -1, 'post_type'=> 'events' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
$list_events= '';
$event_title= get_the_title();
$event_photo= get_post_meta($post->ID, 'event_photo', true);
$list_events.='
<h2>'.$event_title.'</h2>
<p>'.$event_photo.'</p>
';
$list_events= '';
endforeach;
}
function insert_event($atts, $content=null){
$events_all= get_events();
return $events_all;
}
add_shortcode('all_events', 'insert_event');
but, it is not working. Can anyone help?

Related

Loop throuh all members of site in WordPress/WooCommerce

I want to display all members of my site.
As I understand the WooCommerce Membership plugin, the members are stored as custom post type (https://docs.woocommerce.com/document/woocommerce-memberships-data-structure/#section-4) and I could get them with get_posts().
I tried to do so with the following code:
<ul class="my-5">
<?php
global $post;
$args = array( 'post_type' => 'wc_user_membership' );
$posts = get_posts( $args );
foreach ( $posts as $post ) :
setup_postdata( $post ); ?>
<li><?php echo $post->ID; ?></li>
<?php endforeach; wp_reset_postdata(); ?>
</ul>
Unfortunately without any result.
Is there anything wrong with my code?
Updated:
Try to add in your $args array 'numberposts' => -1 and 'post_status' => 'wcm-active' so your code will be:
<ul class="my-5">
<?php
$posts = get_posts( array(
'post_type' => 'wc_user_membership',
'post_status' => 'wcm-active',
'numberposts' => -1,
) );
foreach ( $posts as $post ) :
setup_postdata( $post ); ?>
<li><?php echo $post->ID; ?></li>
<?php endforeach; wp_reset_postdata(); ?>
</ul>
You don't need global $post; as you are using $post in the foreach loop.
Tested and works.

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.

Wordpress output posts with category

I have loop which outputs last 3 posts:
<?php
$args = array( 'numberposts' => 3 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
/////////insert smth
<?php endforeach; ?>
I need to make sure that post category is equal to "vakancy", can i filter only for posts only with this category or i will have to use (if!=) ?
Sure its easy: https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
<?php
$args = array( 'numberposts' => 3, 'category_name' => 'vakancy' );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
/////////insert smth
<?php endforeach; ?>

Modifying loop to show custom fields just for the active/opened post- Wordpress

I need to modify this code to show get custom fields just for the post on which the user currently is.
// this needs to be modified
<?php
global $post;
$args = array('category' => 37, 'post_type' => 'post' );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post);
?>
// end this needs to be modified
<?php if( get_post_meta($post->ID, "Title", true) ): ?>
<?php echo get_post_meta($post->ID, "Title", true); ?></p>
<?php endif; ?>
Because this will be included, I cannot make it just with get_post_meta.
Many thanks!
This ignores the loop and only shows the custom field for the current page, notice get_the_ID():
<?php if( get_post_meta(get_the_ID(), "Title", true) ): ?>
<p><?php echo get_post_meta(get_the_ID(), "Title", true); ?></p>
<?php endif; ?>
<?php
global $post;
$args = array('category' => 37, 'post_type' => 'post' );
$postslist = get_posts( $args );
foreach ($postslist as $post){
setup_postdata($post);
}
?>
And this will output all custom fields named "Title" from within the loop:
<?php
global $post;
$args = array('category' => 37, 'post_type' => 'post' );
$postslist = get_posts( $args );
foreach ($postslist as $post){
setup_postdata($post);
echo('<p>'.get_post_meta($post->ID, "Title", true).'</p>');
}
?>

WP -- Get posts by category?

I'm using this in my page template to get posts by their category:
<?php
if (is_page(19)){
?>
<ul>
<?php
global $post;
$args = array( 'category' => 'Testimonial' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li class="testimonial"><?php the_content(); ?></li><br/>
<?php endforeach; ?>
</ul>
<?php } ?>
but it's retrieving all posts instead. Not just the ones labeled Testimonial. Any idea what I'm doing wrong?
'category_name'=>'this cat' also works but isn't printed in the WP docs
Check here : https://developer.wordpress.org/reference/functions/get_posts/
Note: The category parameter needs to be the ID of the category, and
not the category name.
You can use 'category_name' in parameters.
http://codex.wordpress.org/Template_Tags/get_posts
Note: The category_name parameter needs to be a string, in this case, the category name.
add_shortcode( 'seriesposts', 'series_posts' );
function series_posts( $atts )
{ ob_start();
$myseriesoption = get_option( '_myseries', null );
$type = $myseriesoption;
$args=array( 'post_type' => $type, 'post_status' => 'publish', 'posts_per_page' => 5, 'caller_get_posts'=> 1);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<ul>';
while ($my_query->have_posts()) : $my_query->the_post();
echo '<li><a href="';
echo the_permalink();
echo '">';
echo the_title();
echo '</a></li>';
endwhile;
echo '</ul>';
}
wp_reset_query();
return ob_get_clean(); }
//this will generate a shortcode function to be used on your site [seriesposts]
Create a taxonomy field category (field name = post_category) and import it in your template as shown below:
<?php
$categ = get_field('post_category');
$args = array( 'posts_per_page' => 6,
'category_name' => $categ->slug );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
//your code here
<?php endforeach;
wp_reset_postdata();?>

Categories