Getting only direct child pages in WordPress with get_pages

I am trying to fetch all direct children of a page. But I am getting all children and grand children as well. Any ideas? PHP Source: $args = array( 'child_of' => $post->ID, 'parent ' => $post->ID, 'hierarchical' => 0, 'sort_column' => 'menu_order', 'sort_order' => 'asc' ); $mypages = get_pages( $args ); foreach( $mypages as $post ) { $post_tempalte = the_page_template_part(); get_template_part( 'content' , $post_tempalte ); } My $args should be correct according to the documentation, but it's totally ignoring parent …
Category: Web

Get post id outside loop : Notice: Trying to get property of non-object

I am trying to get the post id outside the loop in functions.php.but what error i am getting : Notice: Trying to get property of non-object in functions.php on line 549 function theme_myeffecto_disable() { global $wp_query; $post_id = $wp_query->post->ID; $showreaction = get_post_meta( $post_id, 'post_reaction_show', true ); $showreaction = isset($showreaction) ? $showreaction : true; var_dump($showreaction); } add_action( 'init', 'theme_myeffecto_disable', 20 ); and $showrating always comes false weather it is true or false :(
Category: Web

Compare ACF date field (if exists) and post date and order DESC

I have an ACF date field (same format as post date) and in my Query I want it to be compared to the publish date of the post: If erstelldatum is not empty use erstelldatum for order If erstelldatum is empty use publish date for order instead Order descending My code is working so far but the order isn`t right. It lists all posts with erstelldatum first followed by those without. I want them to be ordered descending by the …
Category: Web

Display category name of custom posts only once in the loop

I have a custom post type called "Projects" and for it some categories. I would like to display the category name of the posts in the loop but only once. I am using this but the category is being displayed on top of every post. For example if the 2 posts are in the same cetegory, now I see the category name twice in the same loop. How can I solve it? My code: <?php $category = the_category(); ?> <h2 …
Category: Web

Make custom Wordpress comments list

i'm making a custom Wordpress template and I've made this HTML code for single comment template: <div class="comment" id="1"> <div class="useravatar" style="background-image: url('[LINK]')"></div> <div class="textbox"> <div class="dateauthor">[AUTHOR NAME] <span class="date">[DATE]</span></div> <div class="text"> [COMMENT TEXT] </div> </div> </div> But i'm getting only headache to find out how to make a loop o something else where I can use this. Is there any good example for this cases?
Category: Web

get_the_terms() returning wrong results inside of loop

When I use get_the_terms() inside a loop, the whole thing behaves very strangely. Sometimes the terms are included twice or three times and sometimes a post inside of the loop returns only false. I have used get_the_terms() over and over for the last few years without any problems. So i'm quite confused right now. It seems like wp_get_post_terms() and wp_get_post_categories() are getting out the correct data but I would like to know why get_the_terms() is behaving this way inside of …
Category: Web

How Can I use WP_Query to Only Display 1 Post from Custom Post Type if Query Returns Posts with Matching ID in Custom Field

I have searched high and low for a solution and couldn't find anything; hopefully someone here more skilled than myself can lend a hand. I have two testimonial videos (from a video custom post type) displayed randomly every time the page loads. There are different clients that have testimonials and in some cases, there are multiple testimonials from the same client. I have the query working that randomly displays two videos from the custom post type (while filtering out a …
Category: Web

Forcing page to top of the list when using get_pages

I am using get_pages to pull the children pages from a parent and listing them in a similar way to the standard WP loop for pages. This works well using the code referenced below - however.. what I would like to be able to do is to list a page I know the ID for at the top irrespective of its publish date (I am listing the pages in date order). Is this possible? Page ID of the page I …
Topic: loop Wordpress
Category: Web

Attachment Metadata inside of Loop

Basically, I'm trying to call attachment info to display withing a loop, for use in an image gallery. I know I need to use wp_prepare_attachment_for_js($attachment_id) but I can't seem to get anything to show. Here's the HTML: <?php $the_query = new WP_Query(array( 'post_type' => 'attachment', 'post_status' => 'inherit' )); while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <?php attachment_meta = wp_get_attachment($id); echo '<figure class="gallery-photo" itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject" data-groups='["all", "'.$attachment_meta['category_name'].'"]'><a class="photo-link" href="'.wp_get_attachment_image ().'">" itemprop="contentUrl" data-size="'.$attachment_meta[width].'"x"'.$attachment_meta[height].';?>"> <img src="'.wp_get_attachment_url ('fullsize').'" itemprop="thumbnail" /> <figcaption itemprop="caption …
Category: Web

Bootstrap tabs are not being clicked in WordPress loop

I am trying to show some Bootstrap tab contents within a WordPress loop. The tabs are not being clicked. Here are the bootstrap codes: <ul class="citenav nav nav-tabs" id="myTab" role="tablist"> <li class="nav-item"> <a class="nav-link active" id="tab1-tab" data-toggle="tab" href="#tab1-<?php echo get_the_ID(); ?>" role="tab" aria-controls="tab1" aria-selected="true">tab18</a> </li> <li class="nav-item"> <a class="nav-link" id="tab2-tab" data-toggle="tab" href="#tab2-<?php echo get_the_ID(); ?>" role="tab" aria-controls="tab2" aria-selected="false">tab2</a> </li> <li class="nav-item"> <a class="nav-link" id="tab3-tab" data-toggle="tab" href="#tab3-<?php echo get_the_ID(); ?>" role="tab" aria-controls="tab3" aria-selected="false">tab3</a> </li> <li class="nav-item"> <a class="nav-link" id="tab4-tab" data-toggle="tab" href="#tab4-<?php …
Category: Web

Add leading zero to current post display

I am using the next to show in a loop of posts the current post and the total of posts published. $post_query = new WP_Query($args); $postAmount = wp_count_posts( 'post' )->publish; <?php echo $post_query->current_post + 1 ?> / <?php echo $postAmount ?> This returns 1/20, 2/20, etc Does someone know how to add a leading zero so it shows 01/20, 02/20, etc? Thanks a lot in advance
Topic: loop Wordpress
Category: Web

Custom post type loop error: Trying to get property of non-object

A plugin I am using creates a custom post type for articles ie. 'ht_kb'. In my themes front page I have a custom loop displaying recent posts of the standard 'post' type. On the same front page I have another custom loop displaying recent posts of the plugins custom 'ht_kb' post type. The standard post type loop works as expected, but with the custom post type loop I am getting the error as posted below. I am having difficulty understanding …
Category: Web

Filter post by current 2 differents users id

I'm filtering my custom post type based on author. I want all users to be able to: 1 - in the list everyone can see the posts created by the admin. 2 - users can also see their own posts in the list, but not those of other users. I can list by admin id only or current user id only, but not both. Code: function list_cpt_by_author_id(){ $user = new WP_User(get_current_user_id()); $authors_id = array( array('author' => "1"), array('author' => "$user"), …
Category: Web

ACF to select posts not displaying on blog page

I am trying to implement a repeater / post object so I can select posts that I want to displaying on the sidebar. I am using this code: <?php while ( have_rows('top_posts_repeater')) : the_row(); // loop through the repeater fields ?> <?php // set up post object $post_object = get_sub_field('selection'); if( $post_object ) : $post = $post_object; setup_postdata($post); ?> <article class="your-post"> <?php the_title(); ?> <?php the_post_thumbnail(); ?> <?php // whatever post stuff you want goes here ?> </article> <?php wp_reset_postdata(); …
Category: Web

How to get list of posts from permalinks?

I am using an API which tracks metrics on parts of my site. The only useful bit it saves though is the URL (permalink). What is the most efficient way to query up all the posts that match those permalinks, given the fact that I don't have access to the ID's to use post__in with WP_Query.
Category: Web

Injecting content with $wp_query->current_post restarts from zero on paged pages. How to inject content after X posts, regardless of pagination?

I am creating a shortcode that outputs a custom loop. Within the loop while, I am injecting certain content after a particular post count. This is done by checking the current_post number. This particular function is quite long (and filled with commented-out code, from testing). Here is a link to my current version (currently with ALL posts on a single page): https://gist.github.com/Garconis/c93ce89c3d378bfb7f5d8df97acbebc2 This works fine if/when ALL posts (posts_per_page => -1) are showing, but as soon as I add pagination …
Category: Web

Custom Loop and Infinite Scroll

I am using customized version of _s theme where I have Product CPT. In the following files, I need to use Infinite scroll (provided by Jetpack) - archive-products.php taxonomy-[tax-name].php home.php archive.php Now, the problem is most of these templates use custom loop (using WP_Query which ends with pagination after while()). I have used following code in functions.php after activating jetpack and Photon module - function vg_infinite_scroll_init() { add_theme_support( 'infinite-scroll', array( 'container' => 'page-content', 'footer' => 'page', 'type' => 'click', 'render' …
Category: Web

Custom Fields - How to create a list from multi-line entries of a single value

If I have the following name/value pair for a custom field.... ...is there a PHP loop process I can apply that will allow me to display them on the front-end as individual values because there is a line-break between each item? or do I have to create multiple name/value pairs for each item? The end goal in my case is to create a list on the front end of these items that are being pulled from this value area. Thanks …
Category: Web

Custom post type archive pagination with HTML5Blank theme?

Can't get the pagination to work. Here's my archive-work.php: <?php get_header(); ?> <main role="main"> <!-- section --> <section> <h1><?php _e( 'Work', 'html5blank' ); ?></h1> <?php get_template_part('loop', 'work'); ?> <?php get_template_part('pagination'); ?> </section> <!-- /section --> </main> <?php get_sidebar(); ?> <?php get_footer(); ?> loop-work.php: <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $work = array( 'post_type' => 'work' ,'numberposts' => -1 ,'posts_per_page' => 2 ,'paged' => $paged ); $loop = new WP_Query( $work ); if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?> …
Category: Web

AJAX handler throws 400 (Bad request) - why?

I am trying to apply a "Load more posts" function to a post loop, but I am dealing with a 400 Bad request, when admin-ajax.php is referred to. The reference I used is this - https://rudrastyh.com/wordpress/load-more-posts-ajax.html Following function (in functions.php) is passing query parameters to javascript: function wordpress_my_load_more_scripts() { global $wp_query; wp_enqueue_script('jquery'); wp_register_script( 'my_loadmore', get_stylesheet_directory_uri() . '/myloadmore.js', array('jquery') ); wp_localize_script( 'my_loadmore', 'wordpress_loadmore_params', array( 'ajaxurl' => admin_url() . 'admin-ajax.php', 'posts' => json_encode( $wp_query->query_vars ), 'current_page' => get_query_var( 'paged' ) ? get_query_var('paged') …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.