I tried this:
add_action( 'the_post', 'un_post_filter' );
function un_post_filter ( $post ) {
if ($post->post_type == 'page' && $post->post_name == 'mypage') {
$category_name = get_query_var ('category_name');
if (! empty ($category_name)) {
$category = get_category_by_slug($category_name);
$post->post_title .= ": " . $category->name;
$post->title = $post->post_title;
}
}
According to documentation and scarce examples of using action the_post this should change values and changes should be visible in HTML.
Changes on the $post are applied as expected, but HTML still shows unaltered values.
What I am doing wrong?
]]>I am trying to use the plugin in combination with ad-inserter. It works fine on the homepage but if i duplicate the homepage to create another page with a post overview, the WP the_post hook is not called, so the ad-inserter also stops working.
Is there any workaround for this problem?
Normal page:
https://schedules.logistics-manager.com/
Duplicated homepage where the_post is not called anymore and the ad-insterter stops working;
https://schedules.logistics-manager.com/printpage/
Thanks and hope someone can help.
Kind regards
Floris
I am having to create a custom loop to display posts published within a specific timeframe on a non-index, static page. I searched through the web for a clue with no luck.
So far, I wrote the following script:
<?php
global $wp_query, $paged;
$paged = ( get_query_var( ‘paged’ ) ) ? get_query_var( ‘paged’ ) : 1;
$args = array(
‘category_name’ => ‘property-detail’,
‘post_type’ => ‘post’,
‘posts_per_page’ => 3,
‘paged’ => $paged
);
$catquery = new WP_Query($args);
while($catquery->have_posts()) : $catquery->the_post();
echo “HTML mark-up”;
?>
<?php endwhile;
echo ‘<nav class=”pagination”>’.pagination_bar( $catquery ).'</nav>’;
wp_reset_postdata();
?>
I want the loop to iterate through the fourth latest post to the sixth latest post only. Could someone please teach me how that can be done?
Thank you in advance,
Ead
]]>ie.
post 1 X=1, y=4 and external variable z=6 custom calculated field = 11
post 2 x=3, y=4 and external variable z=9 custom calculated field = 16
post 3 x=5, y=4 and external variable z=4 custom calculated field = 13
When these posts are displayed in a list format, I want the following sort order.
3
1
2
I cannot include the external variable and calculated field in the original post, since the external variable will change per user.
]]>Ideally, I’d like to have a bit of html automatically added to the front end of my custom post type every time I publish a new one.
from what I understand, putting this in my child theme’s functions.php should have done it:
add_action( 'the_post', 'teaching_part_one' );
function teaching_part_one(){
?>
<p>custom code</p>
<?php
}
but when I put that in and update it, it will display
“custom code custom code
custom code custom code
custom code custom code”
etc…
in the header of every page on my site, which confuses me, because I thought that would hook into (not sure if i’m using that correctly) the posts on my site.
Would anybody know what I might be doing wrong, and how I might be able to add code that will display in all of my posts automatically, preferably to the child theme?
thanks for reading my post, and thank you in advance for the help!
]]>With this template I have seen a small gallery is generated, which shows products and by-products, and managing a catalog editor called Goods.
When hag click on a product, its features and shows me some sub-products, however I need to show me all byproducts.
Example = On the Menu: Categorias / Laboratorio sample down product characteristics menu Related Products: only shows me 4 when really I have more than 4 sub-products, and thus equal Laboratorio and Orthodoncia.
So far I have not been with the nail, but I’ve managed to do is edit the single-goods.php file but shows only a limited number of posts without names or images
I have noticed that this also depends on two classes: have_posts() and the_post(), but not which of the infinite php folders and files is that class.
As a last attempt, yesterday WordPress update to version 4.4.1-en_US .The results are the same
Please I need help. I’ve been a week with this and the customer has been asking me a lot to the site and is the last thing I need …
]]>This is my code:
$events=new WP_Query( array('post_type'=>'team_list_event') );
if($events->have_posts()){
while($events->have_posts()){
$QID=$events->post->ID;
echo '
<tr><td>
<a href="">'.get_the_title($QID).'</a>
</td></tr>';
$events->the_post();
$i=($i==0)?1:0;
}
echo '</table>';
}
I have 2 posts, ID 6 and ID 22.
ID 6 is skipped and ID 22 is rendered twice.
I’ve obviously misunderstood how the loop works: what am I doing wrong?
I suppose I could set up a query “SELECT ID
,post_type
FROM wp_posts
WHERE post_type
= ‘team_list_event'”, but I want to do it the proper way.
The ‘the_post’ action hook allows developers to modify the post object immediately after being queried and setup.
The post object is passed to this hook by reference so there is no need to return a value.
However, I can’t seem to add an extra item to the post object like this:
function my_the_post_action( $post_object ) {
$post_object->foo = 'bar';
}
add_action( 'the_post', 'my_the_post_action' );
I’ve researched this and gotten into the WP Core code to try and figure it out, but nothing has worked. I thought that maybe I needed to run setup_postdata($post)
after I got the post, but that did not work either.
I don’t know what I’m doing wrong here. Has anybody else seen this problem?
]]>We hired someone to develop an App and they are using JSON to provide the App with data. We installed a plugin to spit out the JSON and everything is great except with a block of code that fills two major parts of our website – The headlines and top 5 stories. It just dumps out Post_contnet and we really need the image to be include in the JSON of it.
Here is the main code that generates everything – (Well there is a lot of backend code that provides us with the options of setting if things are sorted by “views”, “shares” and so forth)
function get_story_list( $m ){
if( $m->story !== "custom" ){
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => $m->order,
'orderby' => 'date'
);
if( $m->order_by !== "date"){
$args["meta_key"] = "post_".$m->order_by;
$args["orderby"] = "meta_value_num";
$args["date_query"] = array(
"after" => '-3 days'
);
}
if( $m->type == "headline" ){
$args['posts_per_page'] = '5';
} else if( $m->type == "list" ){
$args['posts_per_page'] = '6';
} else {
$args['posts_per_page'] = '15';
}
if( strlen($m->author) > 0 ){
$args['author__in'] = explode(",",$m->author);
}
if( strlen($m->cats) > 0 ){
$args['cat'] = $m->cats;
}
if( strlen($m->tags) > 0 ){
$args['tag__in'] = explode(",",$m->tags);
}
$query = new WP_Query($args);
$posts = $query->posts;
} else {
$post_ids = explode(",", $m->post_list);
$posts = array();
foreach( $post_ids as $p ){
$posts[] = get_post( $p );
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
}
}
return $posts;
}
Then it spits out in the template like so –
<?php
$stories = get_story_list( $m );
//var_dump($stories);
?>
<div class="module module-list">
<?php if( count( $stories ) > 0 ){ ?>
<?php
$fp = fopen('json/stories.json', 'w');
fwrite($fp, json_encode($stories));
fclose($fp); ?>
<h2><?php echo $m->title; ?></h2>
<ul>
<?php foreach( $stories as $s ){
$category = get_the_category( $s->ID );
if( $category[0]->parent !== "0" ){
$parent = get_category( $category[0]->parent );
} else {
$parent = $category[0];
}
$color = get_category_color( $parent->term_id );
?>
<li class="module-list-tile <?php echo $color.'-h-grad-l-white'; ?>">
<div class="wrap">
<div class="info">
<span class="<?php echo $color.'-font'; ?>"><?php echo $category[0]->name; ?></span>
<span class="time">| <?php echo date("M d, G:i", strtotime($s->post_date)); ?></span>
</div>
<h3><?php echo $s->post_title; ?>
<div class="author">by
<?php $author_tax = wp_get_post_terms( $s->ID, array("authors") );
if( count( $author_tax ) > 0 ){
echo $author_tax[0]->name;
} ?>
</div>
</h3>
</div>
<a href="<?php echo get_permalink($s->ID); ?>"><span class="span-link <?php echo $color.'-border-m'; ?>"></span><span class="hide"><?php echo $s->post_title; ?></span></a>
</li>
<?php } ?>
</ul>
<?php } ?>
</div>
And sample of the JSON we are returned –
`[{“ID”:38719,”post_author”:”1″,”post_date”:”2014-12-08 03:52:38″,”post_date_gmt”:”2014-12-08 12:52:38″,”post_content”:””,”post_title”:”Doug Dubach Moves Ahead”,”post_excerpt”:””,”post_status”:”publish”,”comment_status”:”open”,”ping_status”:”closed”,”post_password”:””,”post_name”:”doug-dubach-974″,”to_ping”:””,”pinged”:””,”post_modified”:”2014-12-08 05:24:22″,”post_modified_gmt”:”2014-12-08 14:24:22″,”post_content_filtered”:””,”post_parent”:0,”guid”:”http:\/\/localhost:8888\/dev\/?p=38719″,”menu_order”:0,”post_type”:”post”,”post_mime_type”:””,”comment_count”:”0″,”filter”:”raw”,”shares_facebook”:”0″,”shares_twitter”:”0″,”shares_googleplus”:”0″,”shares_pinterest”:”0″,”shares_email”:”0″,”post_views”:”0″,”}]
I’d like to display a different post than the one requested without redirecting the visitor.
For example: www.site.com/title-post1-ID1 displays the content of another post having the ID ID2
ID2 is chosen using some of my visitor properties. Meta (opengraph, meta, etc) must be changed as well. In fact, that’s the entire post that I want to change.
A solution consists in replacing the post object on the fly using a plugin, which must be executed before all other plugins and before executing the template single.php.
/* my plugin */
function my_post_action($post_object)
{
if (/* my conditions */)
{
$post_object = get_post(/* a new post with ID2 */);
}
return $post_object;
}
add_action( 'the_post', 'my_post_action',0 );
This plugin doesn’t work. Questions:
– is it possible to modify the post object with such an action?
– before all plugins and template?
– how to run this action for the current post and not for the posts related in the single page widgets (related post, etc)
I’ve been trying for days. Thank for your help.
Tom
]]>